Skip to content

Commit

Permalink
Make when context specific keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
vipentti committed Dec 14, 2023
1 parent 039fd02 commit 48f0720
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/Visp.Compiler/ParseUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ let mkTokenizerWithArgs args =
args.PopContext()
args.PushContext LexContext.Member

| MATCH ->
if args.CurrentContext = LexContext.LParen then
args.PopContext()
args.PushContext LexContext.Match

| HASH_PAREN
| HASH_BRACKET
| DOT_BRACKET
Expand Down
5 changes: 3 additions & 2 deletions src/Visp.Compiler/Syntax/LexHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type LexContext =
| Default
| LParen
| Member
| Match

type LexContextStack() =
let stack = new Stack<LexContext>()
Expand Down Expand Up @@ -223,7 +224,6 @@ let keywordTokenList =
("type", TYPE)
("union", UNION)
("unquote", UNQUOTE_KW)
("when", WHEN)
("while", WHILE)
("yield", YIELD false)
("yield!", YIELD true) ]
Expand All @@ -233,7 +233,8 @@ let contextSpecificKeywords =
[ ("get", MEMBER_GET)
("set", MEMBER_SET)

]) ]
])
(LexContext.Match, [ ("when", WHEN) ]) ]

let contextSpecificKeywordsMap =
contextSpecificKeywords
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 @@ -10,6 +10,11 @@ namespace ParsingTests

open Xunit

[<VerifyXunit.UsesVerify>]
module ``tests_while_while-1`` =
[<Fact>]
let ``can parse`` () = TestUtils.runTest "tests/while/while-1.visp"

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

#nowarn "0020" // unused results from functions

open Visp.Runtime.Library

let state = { Todo = () }
// line 8 @"while-1.visp"
let inline DiffByOne (lhs: array<'T>) (rhs: array<'T>) =
// line 9 @"while-1.visp"
let mutable index = 0
// line 10 @"while-1.visp"
let mutable loop = true
// line 11 @"while-1.visp"
let mutable diff = 0
// line 13 @"while-1.visp"
while CoreMethods.isTruthy(
if CoreMethods.isTruthy(
loop)
then
// line 13 @"while-1.visp"
CoreMethods.``lt``(index, (lhs.Length))
else
false) do
// line 14 @"while-1.visp"
let lhs =
// line 14 @"while-1.visp"
(lhs.[index])
// line 15 @"while-1.visp"
let rhs =
// line 15 @"while-1.visp"
(rhs.[index])
// line 17 @"while-1.visp"
if CoreMethods.isTruthy(
CoreMethods.``not``(CoreMethods.``eq?``(lhs, rhs)))
then
// line 17 @"while-1.visp"

// line 17 @"while-1.visp"
diff <- inc (diff)
// line 20 @"while-1.visp"
if CoreMethods.isTruthy(
CoreMethods.``gt``(diff, 1))
then
// line 20 @"while-1.visp"

// line 20 @"while-1.visp"
loop <- false
// line 23 @"while-1.visp"
index <- inc (index)
// line 26 @"while-1.visp"
CoreMethods.``eq?``(diff, 1)

// line 29 @"while-1.visp"
let visp_result_todo =
// line 29 @"while-1.visp"
printfn ("OK")
// line 29 @"while-1.visp"
printfn ("%A") (visp_result_todo)

5 changes: 5 additions & 0 deletions tests/Visp.ExecutionTests/ExecutionTests.generated.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ namespace ExecutionTests

open Xunit

[<VerifyXunit.UsesVerify>]
module ``tests_while_while-1`` =
[<Fact>]
let ``can execute`` () = TestUtils.runTest "tests/while/while-1.visp"

[<VerifyXunit.UsesVerify>]
module ``tests_while_while-0`` =
[<Fact>]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
OK
()

ExitCode: 0
29 changes: 29 additions & 0 deletions visp/tests/while/while-1.visp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
;; 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:

(fn inline DiffByOne ([lhs: array<^T>] [rhs: array<^T>])
(mut index 0)
(mut loop true)
(mut diff 0)

(while (and loop (< index (+Length lhs)))
(let lhs (.[index] lhs))
(let rhs (.[index] rhs))

(unless (= lhs rhs)
(set! diff (inc diff)))

(when (> diff 1)
(set! loop false))

(set! index (inc index))
)

(= diff 1))


(printfn "OK")

0 comments on commit 48f0720

Please sign in to comment.