Skip to content

Commit

Permalink
Allow arithmetic operators in macros (+,-,/ and *)
Browse files Browse the repository at this point in the history
  • Loading branch information
vipentti committed Dec 9, 2023
1 parent 6fff799 commit d7592d1
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/Visp.Compiler/Syntax/LexHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ let specialSymbol (s: string) =
| it when it.Length > 1 && it[0] = '+' && isLetter it[1] -> Some(PROP_PLUS s)
| it when it.Length > 1 && it[0] = '.' && isLetter it[1] -> Some(DOT_METHOD s)
| it when it.Length > 1 && it[0] = '-' && isLetter it[1] -> Some(APPLY_METHOD s)
| "+" -> Some(OP_PLUS)
| "-" -> Some(OP_MINUS)
| "/" -> Some(OP_DIV)
| "*" -> Some(OP_MULT)
| _ -> None

let symbolOrKeyword (s: string) =
Expand Down
5 changes: 5 additions & 0 deletions tests/Visp.Compiler.UnitTests/ParsingTests.generated.fs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ module ``tests_macros_struct-example-1`` =
[<Fact>]
let ``can parse`` () = TestUtils.runTest "tests/macros/struct-example-1.visp"

[<VerifyXunit.UsesVerify>]
module ``tests_macros_count-exprs-0`` =
[<Fact>]
let ``can parse`` () = TestUtils.runTest "tests/macros/count-exprs-0.visp"

[<VerifyXunit.UsesVerify>]
module ``tests_macros_syntax-macro-2`` =
[<Fact>]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// This file is auto-generated

#nowarn "0020" // unused results from functions

open Visp.Runtime.Library

let state = { Todo = () }
// line 8 @"count-exprs-0.visp"
let macro_CountExprsExampleTest1 = "__MACRO_INIT__"
// line 15 @"count-exprs-0.visp"
let exprCount =
// line 15 @"count-exprs-0.visp"
(1) +
((1) +
((1) +
((1) +
((1) +
((1) +
(1))))))
// line 17 @"count-exprs-0.visp"
let visp_result_todo =
// line 17 @"count-exprs-0.visp"
printfn ("exprs: %A") (exprCount)
// line 17 @"count-exprs-0.visp"
printfn ("%A") (visp_result_todo)

Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ type Range (start: int64, len: int64) =
// line 8 @"struct-example-1.visp"
member _.End =
// line 8 @"struct-example-1.visp"
CoreMethods.``add``(start, len)
(start) +
(len)
// line 8 @"struct-example-1.visp"
member d.Offset v =
// line 8 @"struct-example-1.visp"
CoreMethods.``sub``(v, (d.Start))
v - (d.Start)

// line 8 @"struct-example-1.visp"
member d.Contains v =
Expand Down Expand Up @@ -78,13 +79,14 @@ type SourceDestMap (dest: int64, src: int64, len: int64) =
((d.Src).Contains src))
then
// line 29 @"struct-example-1.visp"
CoreMethods.``add``(d
(d
|> (fun a1 ->
// line 29 @"struct-example-1.visp"
(a1.Dest))
|> (fun a1 ->
// line 29 @"struct-example-1.visp"
(a1.Start)), ((d.Src).Offset src))
(a1.Start))) +
(((d.Src).Offset src))
else
// line 29 @"struct-example-1.visp"
if CoreMethods.isTruthy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ type Example (x: int, y: int) =
// line 16 @"struct-macro-0.visp"
member d.Sum () =
// line 16 @"struct-macro-0.visp"
CoreMethods.``add``((d.X), (d.Y))
((d.X)) +
((d.Y))

// line 22 @"struct-macro-0.visp"
let instance =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type Example (x: int, y: int) =
// line 22 @"struct-macro-2.visp"
member d.Sum () =
// line 22 @"struct-macro-2.visp"
CoreMethods.``add``((d.X), (d.Y))
((d.X)) +
((d.Y))

// line 22 @"struct-macro-2.visp"
let mkExample x y =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ let name arg =
match arg with
| (a, b) ->
// line 21 @"syntax-macro-0.visp"
CoreMethods.``add``(a, b)
(a) +
(b)
| _ ->
0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ let lambda =
match arg with
| (a , b) ->
// line 28 @"syntax-macro-2.visp"
CoreMethods.``add``(a, b))
(a) +
(b))
// line 29 @"syntax-macro-2.visp"
let named arg =
// line 29 @"syntax-macro-2.visp"
match arg with
| (a , b) ->
// line 29 @"syntax-macro-2.visp"
CoreMethods.``add``(a, b)
(a) +
(b)

// line 32 @"syntax-macro-2.visp"
printfn ("lambda: %i") (lambda (1, 2))
Expand Down
5 changes: 5 additions & 0 deletions tests/Visp.ExecutionTests/ExecutionTests.generated.fs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ module ``tests_macros_struct-example-1`` =
[<Fact>]
let ``can execute`` () = TestUtils.runTest "tests/macros/struct-example-1.visp"

[<VerifyXunit.UsesVerify>]
module ``tests_macros_count-exprs-0`` =
[<Fact>]
let ``can execute`` () = TestUtils.runTest "tests/macros/count-exprs-0.visp"

[<VerifyXunit.UsesVerify>]
module ``tests_macros_syntax-macro-2`` =
[<Fact>]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exprs: 7
()

ExitCode: 0
17 changes: 17 additions & 0 deletions visp/tests/macros/count-exprs-0.visp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
;; Copyright 2023 Ville Penttinen
;; Distributed under the MIT License.
;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md
;;
;; for basic syntax highlighting
;; vim: set syntax=clojure:

(syntax-macro CountExprsExampleTest1
[(_) 0]
[(_ _) 1]
[(_ head tail ...)
(+ 1 (CountExprsExampleTest1 tail ...))]
)

(let exprCount (CountExprsExampleTest1 [] [] [] [] [] [] []))

(printfn "exprs: %A" exprCount)

0 comments on commit d7592d1

Please sign in to comment.