From 4792a296d30c6654a93d8805bb451e955308fc80 Mon Sep 17 00:00:00 2001 From: Emin Arslan Date: Sun, 22 Mar 2026 17:23:49 +0300 Subject: [PATCH] interpreter: minor fixes, changes to the main binary to actually interpret some source code --- bin/comp.ml | 32 +++++++++++++------------------- bin/dune | 2 +- lib/compiler/scope_analysis.ml | 2 +- lib/interpreter/main.ml | 2 +- 4 files changed, 16 insertions(+), 22 deletions(-) diff --git a/bin/comp.ml b/bin/comp.ml index 2f7b820..a15a935 100644 --- a/bin/comp.ml +++ b/bin/comp.ml @@ -1,22 +1,16 @@ - -let def = Parser.parse_str "(define (f) - (let ((x 5)) - (if t (set! x (+ x 1))))) - (define (f) - (define (g y) (* y 2)) - (or (g 5) (g 6))) - (cond - ((> 1 2) 0) - ((> 3 2) 3) - (t -1))";; - let ( let* ) = Result.bind;; -let e = - (*let def = Parser.parse_str "(lambda () (+ x 1) (+ x 1))" in - *) - let* top = Compiler.Syntactic_ast.make (List.hd def) in - Ok (Printf.printf "%s\n" (Compiler.Syntactic_ast.print top)) -let _ = match e with - | Error s -> Printf.printf "%s\n" s +(* Try to interpret some test source code. *) +let some_source = "(define (+ a b) b) + (+ 1 2)";; +(* I don't have any built-in functions at all rn, so we just use a dummy function *) + +let bruh = + let* res = Interpreter.Main.interpret_src some_source in + match res with + | Int x -> Printf.printf "got %d as result\n" x; Ok () + | _ -> Printf.printf "got something else\n" ; Ok () +let _ = + match bruh with + | Error s -> Printf.printf "%s" s | _ -> () diff --git a/bin/dune b/bin/dune index 6924df6..e4478fc 100644 --- a/bin/dune +++ b/bin/dune @@ -1,4 +1,4 @@ (executable (name comp) (public_name ollisp) - (libraries str unix compiler)) + (libraries str unix compiler interpreter)) diff --git a/lib/compiler/scope_analysis.ml b/lib/compiler/scope_analysis.ml index b0407a8..edf0d6d 100644 --- a/lib/compiler/scope_analysis.ml +++ b/lib/compiler/scope_analysis.ml @@ -86,7 +86,7 @@ let resolve_set tbl env sym expr = If a symbol is accessed in a lambda body, that is fine, since that computation is delayed, but for top-level forms that are directly executed we must be strict. - The analyze function is strict by default, until it encounters a lambda, at which + This function is strict by default, until it encounters a lambda, at which point it switches to resolving against all symbols. global_tbl is a table that contains ALL defined symbols, tbl is a table that contains symbols defined only until this point. diff --git a/lib/interpreter/main.ml b/lib/interpreter/main.ml index 329839f..e7290e6 100644 --- a/lib/interpreter/main.ml +++ b/lib/interpreter/main.ml @@ -66,7 +66,7 @@ let interpret program global_syms = let count = Compiler.Scope_analysis.SymbolTable.cardinal global_syms in let globals : runtime_value array = Array.make count Nil in - Ok (interpret_one (Begin program) [] globals) + interpret_one (Begin program) [] globals