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

Feature/day22 #31

Merged
merged 7 commits into from
Dec 23, 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
2 changes: 1 addition & 1 deletion src/Visp.Compiler/CoreParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ let state = { Todo = () }

if returnLast then
res <- Transforms.LastExpressionUpdater.update res
// printfn "%O" res
// eprintfn "%A" res

// use outputStream = new StringWriter()
// writeToStreamNew res outputStream filePath
Expand Down
19 changes: 15 additions & 4 deletions src/Visp.Compiler/Lexer.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,15 @@ let SymbolicExtra = [ '.' '\'' '/']
// https://github.com/dotnet/fsharp/blob/ade794633b4bb495da85c92ee780068b9c6f7344/src/Compiler/lex.fsl#L300
let escape_char = ('\\' ( '\\' | "\"" | '\'' | 'a' | 'f' | 'v' | 'n' | 't' | 'b' | 'r'))

let ident_letter_chars = letter (letter | digit)*

let ident_start_char = letter | SymbolicStartCharacters
let ident_char = ( ident_start_char | digit | SymbolicExtra )
let ident = ident_start_char ident_char*
// let ident_array = letter (letter | digit)* '[' (',')* ']'

let ident_arrow = ident_letter_chars "->" ident_letter_chars?

let anyspace = [' ' '\t' '\r' '\n']
let whitespace = [' ' '\t']+
let newline = '\r' | '\n' | "\r\n"
Expand Down Expand Up @@ -247,13 +251,15 @@ rule token (args: LexArgs) (skip: bool) = parse
| eof { EOF }

// punct
| "||" { BAR_BAR }
| "-||" { BAR_BAR }
| '(' { LPAREN }
| ')' { RPAREN }
| '{' { LBRACE }
| '}' { RBRACE }
| '[' { LBRACKET }
| ']' { RBRACKET }
| "::" { COLON_COLON }
| "::" { COLON_COLON }
| ':' anyspace+ { COLON }
| ',' { COMMA }
| '|' { BAR }
Expand All @@ -272,8 +278,8 @@ rule token (args: LexArgs) (skip: bool) = parse
| "|}" { BAR_BRACE }
| "[|" { BRACKET_BAR }
| "|]" { BAR_BRACKET }
| "(|" { PAREN_BAR }
| "|)" { BAR_PAREN }
| "(|" { PAREN_BAR }
| "|)" { BAR_PAREN }
| ".." { DOTDOT }
| "'" { QUOTE_SYM }
| ".[" { DOT_BRACKET }
Expand Down Expand Up @@ -426,10 +432,11 @@ rule token (args: LexArgs) (skip: bool) = parse
| ">=" { GREATER_EQUALS }
| "<=" { LESS_EQUALS }
| "&&" { AMP_AMP }
| "||" { BAR_BAR }
| "!=" { BANG_EQUALS }
| '=' { EQUALS }

| ident_arrow { SYMBOL (lexeme lexbuf) }

| "->>" { THREAD_LAST }
| "->" { THREAD_FIRST }
| "<" { OP_LESS }
Expand Down Expand Up @@ -506,6 +513,8 @@ and tokenStream (args: LexArgs) (skip: bool) = parse
| "::" anyspace+ { COLON_COLON }
| ':' anyspace+ { COLON }
| ',' { COMMA }
| "||" { SYMBOL (lexeme lexbuf) }
| "-||" { SYMBOL (lexeme lexbuf) }
| '|' { BAR }
| '.' anyspace+ { DOT }
| "#(" { HASH_PAREN }
Expand All @@ -527,6 +536,8 @@ and tokenStream (args: LexArgs) (skip: bool) = parse
| '+' anyspace+ { SYMBOL "+" }
| '/' anyspace+ { SYMBOL "/" }

| ident_arrow { SYMBOL (lexeme lexbuf) }

| "->>" { SYMBOL (lexeme lexbuf) }
| "->" { SYMBOL (lexeme lexbuf) }
| ">=" { SYMBOL (lexeme lexbuf) }
Expand Down
9 changes: 6 additions & 3 deletions src/Visp.Compiler/Parser.fsy
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,9 @@ recover:

symbol_with_ops:
| SYMBOL { Syntax.mkSynSymbol $1 (rhs parseState 1) }
| SYMBOL OP_GREATER { Syntax.mkSynSymbol $"{$1}>" (rhs2 parseState 1 2) }
| SYMBOL OP_GREATER SYMBOL { Syntax.mkSynSymbol $"{$1}>{$3}" (rhs2 parseState 1 3) }
| SYMBOL OP_LESS SYMBOL OP_GREATER { Syntax.mkSynSymbol $"{$1}<{$3}>" (rhs2 parseState 1 4) }
| SYMBOL OP_GREATER { Syntax.mkSynSymbol $"{$1}>" (rhs2 parseState 1 2) }

expr:
| constant %prec prec_constant { SynExpr.Const(fst $1, snd $1) }
Expand Down Expand Up @@ -993,8 +993,10 @@ rev_attributes:
| rev_attributes DOT attribute { $3 :: $1 }

attribute:
| syn_type_name expr
{ { TypeName = $1; ArgExpr = $2; Range = lhs parseState } }
| syn_type_name LPAREN expr RPAREN
{ { TypeName = $1; ArgExpr = $3; Range = lhs parseState } }
| syn_type_name LBRACKET expr RBRACKET
{ { TypeName = $1; ArgExpr = $3; Range = lhs parseState } }
| syn_type_name
{ { TypeName = $1; ArgExpr = Syntax.UnitExpr <| lhs parseState; Range = lhs parseState } }

Expand Down Expand Up @@ -1099,6 +1101,7 @@ syn_type:
{ SynType.Fun ($1, $3, lhs parseState) }
| syn_type arrayTypeSuffixHelp %prec prec_syn_type_array
{ SynType.Array($2, $1, lhs parseState) }
| SEQ { SynType.Ident(Ident("seq", rhs parseState 1)) }
| SYMBOL %prec prec_syn_type
{ let text = $1
if (text = "_") then
Expand Down
1,955 changes: 1,006 additions & 949 deletions src/Visp.Compiler/Syntax/FsLexYaccOutput/Lexer.fs

Large diffs are not rendered by default.

1,716 changes: 870 additions & 846 deletions src/Visp.Compiler/Syntax/FsLexYaccOutput/Parser.fs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/Visp.Compiler/Syntax/LexHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type LexMode =
type LexArgs =
{ diagnosticsLogger: DiagnosticsLogger.DiagnosticsLogger
contextStack: LexContextStack
mutable debugTokens: bool
mutable mode: LexMode
mutable stringNest: LexerInterpolatedStringNesting
mutable interpolationDelimiterLength: int
Expand Down Expand Up @@ -134,6 +135,7 @@ type LexArgs =
let mkDefaultLextArgs () =
{ diagnosticsLogger = DiagnosticsLogger.DiagnosticsThreadStatics.DiagnosticsLogger
contextStack = new LexContextStack()
debugTokens = false
mode = LexMode.Default
depth = 0
interpolationDelimiterLength = 0
Expand Down
51 changes: 40 additions & 11 deletions src/Visp.Compiler/Syntax/SynWriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type SynWriter(writer: CustomFileWriter) =

member d.IndentLevel = d.writer.IndentLevel

member d.IncreaseIndent() = d.writer.IncreaseIndent()

member d.DecreaseIndent() = d.writer.DecreaseIndent()

member this.Write(text: string) = this.writer.Write(text)

member this.Write(text: char) = this.writer.Write(text)
Expand Down Expand Up @@ -824,6 +828,12 @@ module Write =
string w "HashMap.find"
writeCallArgs w [ kw; one ]
| _ -> failwithf "unsupported call %O %A" expr args
| Patterns.SymbolWith "with" ->
match args with
| [ expr; SynExpr.RecordInit(inits, _) ] -> writeRecordInit w st (Some expr) inits
| _ ->
writeExpr w WriteState.Inline expr
writeCallArgs w args
| SynExpr.Symbol name ->
match w.knownMethods.TryFind(Syntax.textOfSymbol name) with
| Some method ->
Expand Down Expand Up @@ -900,7 +910,9 @@ module Write =
writeExpr w st ex
writeCallArgs w args

| _ -> failwithf "not callable %O" expr
| it ->
writeExpr w WriteState.Inline it
writeCallArgs w args


| SynExpr.Symbol sym ->
Expand Down Expand Up @@ -1185,19 +1197,36 @@ module Write =

| SynExpr.RecordInit(inits, range) ->
startExpr w st range
string w "{"
writeRecordInit w st None inits

writeBody
w
(fun w st (SynInit(name, expr, range)) ->
startExpr w st range
symbol w name true
string w " = "
writeExpr w WriteState.Inline expr)
inits
and private writeRecordInit w _ (withExpr: SynExpr option) (inits: SynInit list) =

string w "{"

match withExpr with
| Some(withExpr) ->
w.IncreaseIndent()
newlineIndent w
string w "}"
writeExprInParens w WriteState.Inline withExpr
string w " with"
| None -> ()

writeBody
w
(fun w st (SynInit(name, expr, range)) ->
startExpr w st range
symbol w name true
string w " = "
writeExpr w WriteState.Inline expr)
inits

newlineIndent w
string w "}"

if withExpr.IsSome then
w.DecreaseIndent()

()

and private writeCollection w st (writers: CollectionWriters<'a>) (cl: SynCollection<'a>) =
let (SynCollection(kind, items, range)) = cl
Expand Down
4 changes: 4 additions & 0 deletions src/Visp.Compiler/Utilities/Writer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ module Writer =
//member this.WriteFormat (fmt: Printf.TextWriterFormat<'T>) (args: 'T) =
// fprintf streamWriter fmt args

member _.IncreaseIndent() = indentation <- indentation + 1

member _.DecreaseIndent() = indentation <- indentation - 1

member this.Indent() =
indentation <- indentation + 1

Expand Down
40 changes: 40 additions & 0 deletions tests/Visp.Compiler.UnitTests/ParsingTests.generated.fs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ module ``tests_type_custom-comparison-0`` =
[<Fact>]
let ``can parse`` () = TestUtils.runTest "tests/type/custom-comparison-0.visp"

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

[<VerifyXunit.UsesVerify>]
module ``tests_type_member-fn-0`` =
[<Fact>]
Expand All @@ -70,6 +75,11 @@ module ``tests_type_union-0`` =
[<Fact>]
let ``can parse`` () = TestUtils.runTest "tests/type/union-0.visp"

[<VerifyXunit.UsesVerify>]
module ``tests_type_record-3`` =
[<Fact>]
let ``can parse`` () = TestUtils.runTest "tests/type/record-3.visp"

[<VerifyXunit.UsesVerify>]
module ``tests_type_type-alias-0`` =
[<Fact>]
Expand All @@ -85,6 +95,11 @@ module ``tests_type_record-0`` =
[<Fact>]
let ``can parse`` () = TestUtils.runTest "tests/type/record-0.visp"

[<VerifyXunit.UsesVerify>]
module ``tests_type_record-4`` =
[<Fact>]
let ``can parse`` () = TestUtils.runTest "tests/type/record-4.visp"

[<VerifyXunit.UsesVerify>]
module ``tests_functions_apply-method-args-0`` =
[<Fact>]
Expand All @@ -110,6 +125,11 @@ module ``tests_parsing_match-0`` =
[<Fact>]
let ``can parse`` () = TestUtils.runTest "tests/parsing/match-0.visp"

[<VerifyXunit.UsesVerify>]
module ``tests_parsing_sym-with-arrow-in-macro-0`` =
[<Fact>]
let ``can parse`` () = TestUtils.runTest "tests/parsing/sym-with-arrow-in-macro-0.visp"

[<VerifyXunit.UsesVerify>]
module ``tests_parsing_tuple-0`` =
[<Fact>]
Expand All @@ -135,11 +155,21 @@ module ``tests_parsing_props`` =
[<Fact>]
let ``can parse`` () = TestUtils.runTest "tests/parsing/props.visp"

[<VerifyXunit.UsesVerify>]
module ``tests_parsing_operators-0`` =
[<Fact>]
let ``can parse`` () = TestUtils.runTest "tests/parsing/operators-0.visp"

[<VerifyXunit.UsesVerify>]
module ``tests_parsing_call-tuple-args-0`` =
[<Fact>]
let ``can parse`` () = TestUtils.runTest "tests/parsing/call-tuple-args-0.visp"

[<VerifyXunit.UsesVerify>]
module ``tests_parsing_span-to-int32-0`` =
[<Fact>]
let ``can parse`` () = TestUtils.runTest "tests/parsing/span-to-int32-0.visp"

[<VerifyXunit.UsesVerify>]
module ``tests_parsing_let-values`` =
[<Fact>]
Expand All @@ -150,6 +180,11 @@ module ``tests_parsing_match-1`` =
[<Fact>]
let ``can parse`` () = TestUtils.runTest "tests/parsing/match-1.visp"

[<VerifyXunit.UsesVerify>]
module ``tests_parsing_seq-as-generic-0`` =
[<Fact>]
let ``can parse`` () = TestUtils.runTest "tests/parsing/seq-as-generic-0.visp"

[<VerifyXunit.UsesVerify>]
module ``tests_arrays_array2d-0`` =
[<Fact>]
Expand Down Expand Up @@ -275,6 +310,11 @@ module ``tests_builtin-macros_cond-0`` =
[<Fact>]
let ``can parse`` () = TestUtils.runTest "tests/builtin-macros/cond-0.visp"

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

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

#nowarn "0020" // unused results from functions

open Visp.Runtime.Library

let state = { Todo = () }
// line 8 @"up-macro-0.visp"
let DoingThings () =
// line 9 @"up-macro-0.visp"
let mutable items =
// line 9 @"up-macro-0.visp"
[] |> Set.ofList
// line 11 @"up-macro-0.visp"
for i = 0 to 5 do
// line 12 @"up-macro-0.visp"

// line 12 @"up-macro-0.visp"
items <- Set.add i items
items

// line 17 @"up-macro-0.visp"
let visp_result_todo =
// line 17 @"up-macro-0.visp"
printfn "%A" (DoingThings ())
// line 17 @"up-macro-0.visp"
printfn "%A" visp_result_todo

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

#nowarn "0020" // unused results from functions

open Visp.Runtime.Library

let state = { Todo = () }
// line 8 @"operators-0.visp"
let OrTest a b =
// line 8 @"operators-0.visp"
a || b

// line 9 @"operators-0.visp"
let AndTest a b =
// line 9 @"operators-0.visp"
a && b

// line 11 @"operators-0.visp"
printfn "OrTest %A" (OrTest false false)
// line 12 @"operators-0.visp"
printfn "OrTest %A" (OrTest false true)
// line 13 @"operators-0.visp"
printfn "OrTest %A" (OrTest true false)
// line 14 @"operators-0.visp"
printfn "OrTest %A" (OrTest true true)
// line 16 @"operators-0.visp"
printfn "AndTest %A" (AndTest false false)
// line 17 @"operators-0.visp"
printfn "AndTest %A" (AndTest false true)
// line 18 @"operators-0.visp"
printfn "AndTest %A" (AndTest true false)
// line 19 @"operators-0.visp"
let visp_result_todo =
// line 19 @"operators-0.visp"
printfn "AndTest %A" (AndTest true true)
// line 19 @"operators-0.visp"
printfn "%A" visp_result_todo

Loading