Skip to content

Commit

Permalink
Require !! suffix for macro invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
vipentti committed Dec 3, 2023
1 parent e3c6c07 commit 5c06d75
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 39 deletions.
6 changes: 2 additions & 4 deletions src/Visp.Compiler/CoreParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/Visp.Compiler/FsharpGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion src/Visp.Compiler/Syntax/LexHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion src/Visp.Compiler/Transforms/Helpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 4 additions & 7 deletions src/Visp.Compiler/Transforms/SyntaxMacroExpander.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Original file line number Diff line number Diff line change
Expand Up @@ -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)

Original file line number Diff line number Diff line change
Expand Up @@ -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)

2 changes: 1 addition & 1 deletion visp/tests/macros/syntax-macro-0.visp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
))
])

(matchfnexample! name
(matchfnexample!! name
[(!tuple a b)
(+ a b)]
[_ 0])
Expand Down

0 comments on commit 5c06d75

Please sign in to comment.