diff --git a/first-interpreter/.gitignore b/first-interpreter/.gitignore index e35d885..a863071 100644 --- a/first-interpreter/.gitignore +++ b/first-interpreter/.gitignore @@ -1 +1,2 @@ _build +interpreter diff --git a/first-interpreter/Makefile b/first-interpreter/Makefile new file mode 100644 index 0000000..2ea1f7b --- /dev/null +++ b/first-interpreter/Makefile @@ -0,0 +1,36 @@ +ifndef VERBOSE +.SILENT: +endif + +CC = gcc +CFLAGS = -Wall -Wextra -ggdb + +.DEFAULT_TARGET = interpreter + +interpreter: src/main.c build/run.o + $(CC) $(CFLAGS) $< -o $@ build/run.o build/ast.o build/lex.o build/parse.o build/execute.o + +build/ast.o: src/ast.c src/ast.h + mkdir -p build/ + $(CC) $(CFLAGS) -c $< -o $@ + +build/lex.o: src/lex.c src/lex.h + mkdir -p build/ + $(CC) $(CFLAGS) -c $< -o $@ + +build/parse.o: src/parse.c src/parse.h build/ast.o build/lex.o + $(CC) $(CFLAGS) -c $< -o $@ + +build/execute.o: src/execute.c src/execute.h build/ast.o + $(CC) $(CFLAGS) -c $< -o $@ + +build/run.o: src/run.c src/run.h build/ast.o build/lex.o build/parse.o build/execute.o + $(CC) $(CFLAGS) -c $< -o $@ + +.PHONY clean run: +clean: + rm -rf build + rm -rf interpreter + +run: interpreter + ./interpreter program.code diff --git a/first-interpreter/build.hs b/first-interpreter/build.hs deleted file mode 100755 index d24ec78..0000000 --- a/first-interpreter/build.hs +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env cabal -{- cabal: -build-depends: - base, - bytestring, - cereal, - hspec, - process, - shake, --} --- initial version taken from https://shakebuild.com/manual -import Development.Shake -import Development.Shake.FilePath -import Development.Shake.Util - -cc = "gcc" -flags = "-ggdb" -clearCcCache = removeFilesAfter "build" ["//*"] - -main :: IO () -main = shakeArgs shakeOptions{shakeFiles="_build"} $ do - want ["_build/interpreter" <.> exe] - - phony "clean" $ do - putInfo "Cleaning files in _build" - removeFilesAfter "_build" ["//*"] - clearCcCache - removeFilesAfter "newdist" ["//*"] - cmd_ "cabal" "clean" - - phony "run" $ do - need ["_build/interpreter" <.> exe] - putInfo "Running interpreter" - cmd_ "_build/interpreter" ["program.code"] - - "_build/interpreter" <.> exe %> \out -> do - cs <- getDirectoryFiles "src" ["//*.c"] - let os = ["_build" "normal" "src" c -<.> "o" | c <- cs] - need os - cmd_ (cc <> " " <> flags <> " -o") [out] os - - "_build/normal//*.o" %> \out -> do - let c = dropDirectory1 $ dropDirectory1 $ out -<.> "c" - let m = out -<.> "m" - cmd_ (cc <> " " <> flags <> " -c") [c] "-o" [out] "-MMD -MF" [m] - neededMakefileDependencies m diff --git a/first-interpreter/src/execute.c b/first-interpreter/src/execute.c index 0e18459..8886234 100644 --- a/first-interpreter/src/execute.c +++ b/first-interpreter/src/execute.c @@ -86,9 +86,9 @@ int* lookup(char* name, Memory memory, Environment env) { int eval_expr(Expr expr, Memory memory, Environment env) { switch (expr.tag) { + default: case LITERAL: { return expr.data.integer; - break; } case VARIABLE: { int* result = lookup(expr.data.variable, memory, env); @@ -98,7 +98,6 @@ int eval_expr(Expr expr, Memory memory, Environment env) { } else { return *result; } - break; } case FUNCTION: { if (strcmp(expr.data.function.name, "print") == 0) { @@ -131,7 +130,6 @@ int eval_expr(Expr expr, Memory memory, Environment env) { } } return 0; - break; } } } diff --git a/first-interpreter/src/main.c b/first-interpreter/src/main.c index 55ec140..930cfd0 100644 --- a/first-interpreter/src/main.c +++ b/first-interpreter/src/main.c @@ -1,6 +1,6 @@ #include #include -#include "interpreter.h" +#include "run.h" int main(int argc, char** argv) { // Read file diff --git a/first-interpreter/src/interpreter.c b/first-interpreter/src/run.c similarity index 100% rename from first-interpreter/src/interpreter.c rename to first-interpreter/src/run.c diff --git a/first-interpreter/src/interpreter.h b/first-interpreter/src/run.h similarity index 100% rename from first-interpreter/src/interpreter.h rename to first-interpreter/src/run.h