From 5c06d7500dfa7b8feda63415eed8d43c65955766 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Sun, 3 Dec 2023 20:13:18 +0200 Subject: [PATCH] Require !! suffix for macro invocations --- src/Visp.Compiler/CoreParser.fs | 6 ++---- src/Visp.Compiler/FsharpGenerator.fs | 1 + src/Visp.Compiler/Syntax/LexHelpers.fs | 2 +- src/Visp.Compiler/Transforms/Helpers.fs | 2 +- .../Transforms/SyntaxMacroExpander.fs | 11 ++++------- ...n parse tests_atom_atom-0.visp.verified.txt | 18 +++++++++--------- ... tests_examples_example-7.visp.verified.txt | 14 +++++++------- ...parse tests_while_while-0.visp.verified.txt | 18 +++++++++--------- visp/tests/macros/syntax-macro-0.visp | 2 +- 9 files changed, 35 insertions(+), 39 deletions(-) diff --git a/src/Visp.Compiler/CoreParser.fs b/src/Visp.Compiler/CoreParser.fs index 5724557..1cdee72 100644 --- a/src/Visp.Compiler/CoreParser.fs +++ b/src/Visp.Compiler/CoreParser.fs @@ -84,12 +84,10 @@ let state = { Todo = () } | LPAREN | LBRACE | LBRACKET - | HASH_BRACE -> - args.NestIfNotDefault () + | HASH_BRACE -> args.NestIfNotDefault() | RPAREN | RBRACE - | RBRACKET -> - args.UnnestIfNotDefault () + | RBRACKET -> args.UnnestIfNotDefault() | _ -> () next diff --git a/src/Visp.Compiler/FsharpGenerator.fs b/src/Visp.Compiler/FsharpGenerator.fs index 5cba761..88ced6d 100644 --- a/src/Visp.Compiler/FsharpGenerator.fs +++ b/src/Visp.Compiler/FsharpGenerator.fs @@ -125,6 +125,7 @@ type FsharpGenerator(fs: IFileSystem, dir: string) = member this.WriteVispFiles(files: VispFile list) = let dir = this.fs.Directory.CreateDirectory this.dir let existingFiles = dir.GetFiles("*.fs", SearchOption.TopDirectoryOnly) + for file in existingFiles do file.Delete() diff --git a/src/Visp.Compiler/Syntax/LexHelpers.fs b/src/Visp.Compiler/Syntax/LexHelpers.fs index 7986743..672293f 100644 --- a/src/Visp.Compiler/Syntax/LexHelpers.fs +++ b/src/Visp.Compiler/Syntax/LexHelpers.fs @@ -165,7 +165,7 @@ let symbolOrKeyword (s: string) = match tryGetKeyword s with | Some(tok) -> tok | None -> - if s.EndsWith('!') then + if s.EndsWith("!!") then MACRO_NAME(s.TrimEnd('!')) else SYMBOL(s) diff --git a/src/Visp.Compiler/Transforms/Helpers.fs b/src/Visp.Compiler/Transforms/Helpers.fs index a77f125..f5e0f8d 100644 --- a/src/Visp.Compiler/Transforms/Helpers.fs +++ b/src/Visp.Compiler/Transforms/Helpers.fs @@ -143,7 +143,7 @@ let rec transform (func: SynExpr -> SynExpr) expr = func result -let runTransforms1 (expr: SynExpr) (tfs: (SynExpr -> SynExpr) seq) = +let runTransforms1 (expr: SynExpr) (tfs: (SynExpr -> SynExpr) seq) = let flip f a b = f b a tfs |> Seq.fold (flip transform) expr diff --git a/src/Visp.Compiler/Transforms/SyntaxMacroExpander.fs b/src/Visp.Compiler/Transforms/SyntaxMacroExpander.fs index 01ac783..d855581 100644 --- a/src/Visp.Compiler/Transforms/SyntaxMacroExpander.fs +++ b/src/Visp.Compiler/Transforms/SyntaxMacroExpander.fs @@ -43,7 +43,7 @@ let private evaluatePatterns (body: SynMacroBody) (pats: BoundPattern list) = let findPattern (f: SynMacroBody) (pats: BoundPattern list) = match f with | SynMacroBody.Symbol sym -> - printfn "looking for %A in\n%A" sym pats + // printfn "looking for %A in\n%A" sym pats let found = pats @@ -175,15 +175,12 @@ let private evaluatePatterns (body: SynMacroBody) (pats: BoundPattern list) = let result = raw_expr getTokens lexbuf - printfn "RESULT %A" result + // printfn "RESULT %A" result result -let private expandSynMacro - (SynMacro(_, cases, _) as macro) - (SynMacroCall(_, args, _) as call) - = - printfn "todo %A -> %A" macro call +let private expandSynMacro (SynMacro(_, cases, _) as macro) (SynMacroCall(_, args, _) as call) = + // printfn "todo %A -> %A" macro call let pat = cases |> List.tryFind (matchesCase args) diff --git a/tests/Visp.Compiler.UnitTests/snapshots/ParsingTests.can parse tests_atom_atom-0.visp.verified.txt b/tests/Visp.Compiler.UnitTests/snapshots/ParsingTests.can parse tests_atom_atom-0.visp.verified.txt index 29b0de7..b38276d 100644 --- a/tests/Visp.Compiler.UnitTests/snapshots/ParsingTests.can parse tests_atom_atom-0.visp.verified.txt +++ b/tests/Visp.Compiler.UnitTests/snapshots/ParsingTests.can parse tests_atom_atom-0.visp.verified.txt @@ -5,24 +5,24 @@ open Visp.Runtime.Library let state = { Todo = () } -// line 2 @"atom-0.visp" +// line 8 @"atom-0.visp" let inc (v: Value) = - // line 3 @"atom-0.visp" + // line 9 @"atom-0.visp" Value.from ((1) + (unwrapInt (v))) -// line 4 @"atom-0.visp" +// line 10 @"atom-0.visp" let count = - // line 4 @"atom-0.visp" + // line 10 @"atom-0.visp" Value.atom(Value.from(0)) -// line 6 @"atom-0.visp" +// line 12 @"atom-0.visp" printfn ("count is %O") (deref (count)) -// line 8 @"atom-0.visp" +// line 14 @"atom-0.visp" CoreMethods.``swap!``(Value.from(count), inc) -// line 10 @"atom-0.visp" +// line 16 @"atom-0.visp" let visp_result_todo = - // line 10 @"atom-0.visp" + // line 16 @"atom-0.visp" printfn ("count is %O") (deref (count)) -// line 10 @"atom-0.visp" +// line 16 @"atom-0.visp" printfn ("%A") (visp_result_todo) diff --git a/tests/Visp.Compiler.UnitTests/snapshots/ParsingTests.can parse tests_examples_example-7.visp.verified.txt b/tests/Visp.Compiler.UnitTests/snapshots/ParsingTests.can parse tests_examples_example-7.visp.verified.txt index 9d35d75..0885d71 100644 --- a/tests/Visp.Compiler.UnitTests/snapshots/ParsingTests.can parse tests_examples_example-7.visp.verified.txt +++ b/tests/Visp.Compiler.UnitTests/snapshots/ParsingTests.can parse tests_examples_example-7.visp.verified.txt @@ -5,23 +5,23 @@ open Visp.Runtime.Library let state = { Todo = () } -// line 1 @"example-7.visp" +// line 8 @"example-7.visp" let values = - // line 2 @"example-7.visp" + // line 9 @"example-7.visp" let temp = Vector(4) temp.Add(Value.from(1)) temp.Add(Value.from(2)) temp.Add(Value.from("foo")) temp.Add(Value.from("bar")) temp -// line 4 @"example-7.visp" +// line 11 @"example-7.visp" printfn ("values are %O") (values) -// line 5 @"example-7.visp" +// line 12 @"example-7.visp" CoreMethods.``vector-push!``(state, Value.from(values), Value.from(4), Value.from(5), Value.from(6), Value.from(7)) -// line 6 @"example-7.visp" +// line 13 @"example-7.visp" let visp_result_todo = - // line 6 @"example-7.visp" + // line 13 @"example-7.visp" printfn ("values after update are %O") (values) -// line 6 @"example-7.visp" +// line 13 @"example-7.visp" printfn ("%A") (visp_result_todo) diff --git a/tests/Visp.Compiler.UnitTests/snapshots/ParsingTests.can parse tests_while_while-0.visp.verified.txt b/tests/Visp.Compiler.UnitTests/snapshots/ParsingTests.can parse tests_while_while-0.visp.verified.txt index 73e0dee..f43071e 100644 --- a/tests/Visp.Compiler.UnitTests/snapshots/ParsingTests.can parse tests_while_while-0.visp.verified.txt +++ b/tests/Visp.Compiler.UnitTests/snapshots/ParsingTests.can parse tests_while_while-0.visp.verified.txt @@ -5,24 +5,24 @@ open Visp.Runtime.Library let state = { Todo = () } -// line 2 @"while-0.visp" +// line 9 @"while-0.visp" let inc (v: Value) = - // line 3 @"while-0.visp" + // line 10 @"while-0.visp" Value.from ((1) + (unwrapInt (v))) -// line 5 @"while-0.visp" +// line 12 @"while-0.visp" let count = - // line 5 @"while-0.visp" + // line 12 @"while-0.visp" Value.atom(Value.from(0)) -// line 6 @"while-0.visp" +// line 13 @"while-0.visp" let visp_result_todo = - // line 6 @"while-0.visp" + // line 13 @"while-0.visp" while CoreMethods.isTruthy(CoreMethods.``lt``(deref (count), 5)) do - // line 7 @"while-0.visp" + // line 14 @"while-0.visp" printfn ("in loop %O") (deref (count)) - // line 8 @"while-0.visp" + // line 15 @"while-0.visp" CoreMethods.``swap!``(Value.from(count), inc) -// line 6 @"while-0.visp" +// line 13 @"while-0.visp" printfn ("%A") (visp_result_todo) diff --git a/visp/tests/macros/syntax-macro-0.visp b/visp/tests/macros/syntax-macro-0.visp index c83dccf..dbbd64d 100644 --- a/visp/tests/macros/syntax-macro-0.visp +++ b/visp/tests/macros/syntax-macro-0.visp @@ -15,7 +15,7 @@ )) ]) -(matchfnexample! name +(matchfnexample!! name [(!tuple a b) (+ a b)] [_ 0])