Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement aoc2023 day8 and language improvements #12

Merged
merged 7 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,21 @@ public T From<T>()

public Solution CurrentSolution => From<IHazSolution>().Solution;

public IEnumerable<Project> TestProjects => CurrentSolution.GetAllProjects("*Tests*");
public bool IsMacOs =>
System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX);

public bool IsWindows =>
System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);

public IEnumerable<Project> UnitTests =>
CurrentSolution.GetAllProjects("*UnitTests");

public IEnumerable<Project> ExecutionTests =>
CurrentSolution.GetAllProjects("*ExecutionTests");

// Run only unit tests in CI on Windows & MacOS beecause execution tests take a while.
public IEnumerable<Project> TestProjects =>
IsServerBuild && (IsMacOs || IsWindows) ? UnitTests : UnitTests.Concat(ExecutionTests);

bool IUseCsharpier.UseGlobalTool => false;
bool IUseFantomas.UseGlobalTool => false;
Expand Down
5 changes: 3 additions & 2 deletions src/Visp.Compiler/Parser.fsy
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ let parse_error_rich = Some(fun (ctxt: ParseErrorContext<_>) ->
%token OPEN MODULE
%token FN FNSTAR LET LETSTAR MUT SET DOT NEW DOT_BRACKET DOT_PLUS IF_KW BEGIN_KW DO_KW QUOTE_KW UNQUOTE_KW SPLICE_UNQUOTE_KW QUASIQUOTE_KW
%token ATOM_KW DEREF_KW WHILE TYPE RECORD MEMBER MEMBERS MEMBERFN OVERRIDE MACRO MATCH WHEN CONS CONCAT REQUIRE
%token THREAD_FIRST THREAD_LAST SYNTAX_MACRO SEQ YIELD UNION
%token THREAD_FIRST THREAD_LAST SYNTAX_MACRO SEQ UNION
%token INLINE REC RINIT
%token <bool> YIELD
%token <int> INT32
%token <int64> INT64
%token <decimal> DECIMAL
Expand Down Expand Up @@ -311,7 +312,7 @@ parens_expr:
| BANG_VEC expr_list { SynExpr.FsVec($2, lhs parseState)}

| SEQ expr_list { SynExpr.FsSeq($2, lhs parseState) }
| YIELD expr { SynExpr.FsYield($2, lhs parseState) }
| YIELD expr { SynExpr.FsYield($2, $1, lhs parseState) }

macro_call_expr:
| macro_call { SynExpr.SyntaxMacroCall($1) }
Expand Down
2,289 changes: 1,145 additions & 1,144 deletions src/Visp.Compiler/Syntax/FsLexYaccOutput/Parser.fs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/Visp.Compiler/Syntax/FsLexYaccOutput/Parser.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ type token =
| DECIMAL of (decimal)
| INT64 of (int64)
| INT32 of (int)
| YIELD of (bool)
| INLINE
| REC
| RINIT
| THREAD_FIRST
| THREAD_LAST
| SYNTAX_MACRO
| SEQ
| YIELD
| UNION
| ATOM_KW
| DEREF_KW
Expand Down Expand Up @@ -104,14 +104,14 @@ type tokenId =
| TOKEN_DECIMAL
| TOKEN_INT64
| TOKEN_INT32
| TOKEN_YIELD
| TOKEN_INLINE
| TOKEN_REC
| TOKEN_RINIT
| TOKEN_THREAD_FIRST
| TOKEN_THREAD_LAST
| TOKEN_SYNTAX_MACRO
| TOKEN_SEQ
| TOKEN_YIELD
| TOKEN_UNION
| TOKEN_ATOM_KW
| TOKEN_DEREF_KW
Expand Down
3 changes: 2 additions & 1 deletion src/Visp.Compiler/Syntax/LexHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ let keywordTokenList =
("unquote", UNQUOTE_KW)
("when", WHEN)
("while", WHILE)
("yield", YIELD) ]
("yield", YIELD false)
("yield!", YIELD true) ]

let keywordToTokenMap = keywordTokenList |> Map.ofList

Expand Down
10 changes: 10 additions & 0 deletions src/Visp.Compiler/Syntax/ParseHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ let parseChar (text: string) =
let span = text.AsSpan().Slice(2)
parseCharSpan span

let charToParseable (ch: char) =
match ch with
| '\n' -> "#\\lf"
| '\r' -> "#\\cr"
| '\t' -> "#\\tab"
| ' ' -> "#\\space"
| '\\' -> "#\\\\"
| it when it = (char 8) -> "#\\backspace"
| it when it = '\u0000' -> "#\\nul"
| it -> $"#\\{it}"

//------------------------------------------------------------------------
// Parsing: continuations for whitespace tokens
Expand Down
52 changes: 26 additions & 26 deletions src/Visp.Compiler/Syntax/SynWriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,9 @@ module Write =
writeBody w writeExpr body
()

| SynExpr.FsYield(expr, range) ->
| SynExpr.FsYield(expr, bang, range) ->
startExpr w st range
string w "yield "
if bang then string w "yield! " else string w "yield "
writeExprInParens w WriteState.Inline expr

| SynExpr.FsSeq(exprs, range) ->
Expand Down Expand Up @@ -779,9 +779,16 @@ module Write =
| SynExpr.Symbol name ->
match w.knownMethods.TryFind(Syntax.textOfSymbol name) with
| Some method ->
fmtprintf w "%s.``%s``(" method.DeclaringType.Name method.Name

let mutable parens = false

fmtprintf w "%s.``%s``" method.DeclaringType.Name method.Name

if isVariableArgMethod method then
parens <- true
string w "("


string w "state"

if not args.IsEmpty then
Expand All @@ -795,6 +802,9 @@ module Write =
string wt ")")
args
else if hasSingleValueArrayTypeArg method then
parens <- true
string w "("

writeArgComma
w
(fun wt stt ex ->
Expand All @@ -803,8 +813,15 @@ module Write =
string wt ")")
args
else if hasParamArrayAttribute method then
parens <- true
string w "("

writeSeq w WriteState.Arg (flip string ", ") writeExpr args
else

// TODO: Support
parens <- true
string w "("
let parameters = method.GetParameters()
let zipped = Seq.zip args parameters

Expand All @@ -824,7 +841,8 @@ module Write =

()

char w ')'
if parens then
char w ')'
| None ->
symbol w name true
writeCallArgs w args
Expand Down Expand Up @@ -1270,27 +1288,17 @@ module Write =
startExpr w st r

match args with
| [] -> string w "1"
| [] -> string w "LanguagePrimitives.GenericOne"
| [ one ] -> writeExpr w st one
| rest -> writeSeq w WriteState.Inline (flip string " * ") writeExpr rest
| SynOp.Div(args, r) ->
startExpr w st r

match args with
| [ one ] ->
string w "1.0m / (decimal "
writeExpr w WriteState.Inline one
string w ")"
| rest ->
writeSeq
w
WriteState.Inline
(flip string " / ")
(fun w st a ->
string w "(decimal "
writeExpr w st a
string w ")")
rest
string w "LanguagePrimitives.GenericOne / "
writeExprInParens w WriteState.Inline one
| rest -> writeSeq w WriteState.Inline (flip string " / ") writeExprInParens rest
| SynOp.Minus(args, r) ->
startExpr w st r

Expand All @@ -1309,15 +1317,7 @@ module Write =
string w "(fun "
writeArgsOrEmpty w args
string w " ->"

// if body.Length = 1 then
// space w
// for ex in body do
// writeExpr w wsNone ex
// else
// use _ = withIndent w false
writeBody w writeExpr body

string w ")"
()

Expand Down
2 changes: 1 addition & 1 deletion src/Visp.Compiler/Syntax/Syntax.fs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ type SynExpr =
| FsSet of exprs: SynExpr list * range: range
| FsVec of exprs: SynExpr list * range: range
| FsSeq of exprs: SynExpr list * range: range
| FsYield of expr: SynExpr * range: range
| FsYield of expr: SynExpr * bang: bool * range: range
| List of exprs: SynExpr list * range: range
| Vector of exprs: SynExpr list * range: range
| HashMap of exprs: SynExpr list * range: range
Expand Down
2 changes: 1 addition & 1 deletion src/Visp.Compiler/Transforms/Helpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ let rec transform (func: SynExpr -> SynExpr) expr =
| SynExpr.Pair(lhs, rhs, range) ->
SynExpr.Pair(bound_transform lhs, bound_transform rhs, range)
| SynExpr.Tuple(exprs, range) -> SynExpr.Tuple(List.map bound_transform exprs, range)
| SynExpr.FsYield(exprs, range) -> SynExpr.FsYield(bound_transform exprs, range)
| SynExpr.FsYield(exprs, b, range) -> SynExpr.FsYield(bound_transform exprs, b, range)
| SynExpr.FsSeq(exprs, range) -> SynExpr.FsSeq(List.map bound_transform exprs, range)
| SynExpr.FsSet(exprs, range) -> SynExpr.FsSet(List.map bound_transform exprs, range)
| SynExpr.FsArray(exprs, range) -> SynExpr.FsArray(List.map bound_transform exprs, range)
Expand Down
2 changes: 1 addition & 1 deletion src/Visp.Compiler/Transforms/SyntaxMacroExpander.fs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ let private evaluatePatterns
| SynMacroBody.Const(c, _) ->
match c with
| SynConst.Bool v -> res.Add(if v then TRUE else FALSE)
| SynConst.Char ch -> res.Add(CHAR(ch.ToString()))
| SynConst.Char ch -> res.Add(CHAR(ParseHelpers.charToParseable ch))
| SynConst.Decimal dec -> res.Add(DECIMAL dec)
| SynConst.Int32 dec -> res.Add(INT32 dec)
| SynConst.Int64 dec -> res.Add(INT64 dec)
Expand Down
3 changes: 1 addition & 2 deletions src/Visp.Compiler/Transforms/Traversal.fs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ let depthFirstExprsUntilFalse (pred: SynExpr -> bool) (expr: SynExpr) =
yield! loop name
yield! loop value

| SynExpr.FsYield(exprs, _) -> yield! loop exprs

| SynExpr.FsYield(exprs, _, _) -> yield! loop exprs
| SynExpr.LambdaDef(SynLambda(_, exprs, _))
| SynExpr.Begin(exprs, _, _)
| SynExpr.New(_, exprs, _)
Expand Down
2 changes: 2 additions & 0 deletions src/Visp.LanguageServer/LanguageServer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ let findAllSymbolDetails (syms: ResizeArray<_>) expr =
Syntax.rangeOfName name |> textRangeToSyntaxRange
)
)
| SynExpr.Symbol sym ->
syms.Add(SymbolDetails.Variable(sym.Text, false, sym.Range |> textRangeToSyntaxRange))
| _ -> ()

()
Expand Down
2 changes: 2 additions & 0 deletions src/Visp.Runtime.Library/CoreMethods.fs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type CoreMethods =

static member inline div([<ParamArray>] args: 'a[]) : 'a = args |> Array.reduce (/)

static member inline rem_impl (lhs: 'a) (rhs: 'a) : 'a = lhs % rhs

static member inline ``null?``<'a when 'a: null and 'a: equality>(v: 'a) = v = null

static member ``eq?``<'a when 'a: equality>(lhs: 'a, rhs: 'a) = lhs = rhs
Expand Down
10 changes: 10 additions & 0 deletions tests/Visp.Compiler.UnitTests/ParsingTests.generated.fs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ module ``tests_chars_char-0`` =
[<Fact>]
let ``can parse`` () = TestUtils.runTest "tests/chars/char-0.visp"

[<VerifyXunit.UsesVerify>]
module ``tests_chars_char-2`` =
[<Fact>]
let ``can parse`` () = TestUtils.runTest "tests/chars/char-2.visp"

[<VerifyXunit.UsesVerify>]
module ``tests_hashmap_hashmap-0`` =
[<Fact>]
Expand Down Expand Up @@ -205,6 +210,11 @@ module ``tests_macros_cond-macro-1`` =
[<Fact>]
let ``can parse`` () = TestUtils.runTest "tests/macros/cond-macro-1.visp"

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

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

#nowarn "0020" // unused results from functions

open Visp.Runtime.Library

let state = { Todo = () }
// line 9 @"char-2.visp"
let visp_result_todo =
// line 9 @"char-2.visp"
printfn ("Chars are %A") (('=', '|', '<', '>'))
// line 9 @"char-2.visp"
printfn ("%A") (visp_result_todo)

Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ printfn ("* is %i") (a * b * 52)
// line 16 @"example-5.visp"
printfn ("* is %i") (2)
// line 17 @"example-5.visp"
printfn ("* is %i") (1)
printfn ("* is %i") (LanguagePrimitives.GenericOne)
// line 19 @"example-5.visp"
printfn ("- is %i") (a - b - 52)
// line 20 @"example-5.visp"
printfn ("- is %i") (-2)
// line 22 @"example-5.visp"
printfn ("/ is %f") ((decimal a) / (decimal b) / (decimal 52))
printfn ("/ is %A") ((a) / (b) / (52))
// line 23 @"example-5.visp"
printfn ("/ is %A") (LanguagePrimitives.GenericOne / (2))
// line 25 @"example-5.visp"
printfn ("/ is %A") ((decimal (a)) / (decimal (b)) / (decimal (52.0)))
// line 26 @"example-5.visp"
let visp_result_todo =
// line 23 @"example-5.visp"
printfn ("/ is %f") (1.0m / (decimal 2))
// line 23 @"example-5.visp"
// line 26 @"example-5.visp"
printfn ("/ is %A") (LanguagePrimitives.GenericOne / (2))
// line 26 @"example-5.visp"
printfn ("%A") (visp_result_todo)

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// This file is auto-generated

#nowarn "0020" // unused results from functions

open Visp.Runtime.Library

let state = { Todo = () }
// line 8 @"chars-in-macros-0.visp"
let macro_PrintChars = "__MACRO_INIT__"
// line 13 @"chars-in-macros-0.visp"
printfn ("Values: %A") ([('a', '=', 'B', '\n', ' ', '\t')])
// line 15 @"chars-in-macros-0.visp"
printfn ("%A") ('a')
// line 16 @"chars-in-macros-0.visp"
printfn ("%A") ('\n')
// line 17 @"chars-in-macros-0.visp"
printfn ("%A") (' ')
// line 18 @"chars-in-macros-0.visp"
let visp_result_todo =
// line 18 @"chars-in-macros-0.visp"
printfn ("%A") ('\t')
// line 18 @"chars-in-macros-0.visp"
printfn ("%A") (visp_result_todo)

10 changes: 10 additions & 0 deletions tests/Visp.ExecutionTests/ExecutionTests.generated.fs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ module ``tests_chars_char-0`` =
[<Fact>]
let ``can execute`` () = TestUtils.runTest "tests/chars/char-0.visp"

[<VerifyXunit.UsesVerify>]
module ``tests_chars_char-2`` =
[<Fact>]
let ``can execute`` () = TestUtils.runTest "tests/chars/char-2.visp"

[<VerifyXunit.UsesVerify>]
module ``tests_hashmap_hashmap-0`` =
[<Fact>]
Expand Down Expand Up @@ -205,6 +210,11 @@ module ``tests_macros_cond-macro-1`` =
[<Fact>]
let ``can execute`` () = TestUtils.runTest "tests/macros/cond-macro-1.visp"

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

[<VerifyXunit.UsesVerify>]
module ``tests_macros_cond-macro-0`` =
[<Fact>]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Chars are ('=', '|', '<', '>')
()

ExitCode: 0
Loading