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 day5 and language additions #5

Merged
merged 15 commits into from
Dec 6, 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
4 changes: 1 addition & 3 deletions src/Visp.Cli/CliMain.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ let main args =
let filePath = fs.Path.GetFullPath args.[0]
let cwd = fs.Path.GetDirectoryName filePath

let coreLibs = [ VispFile.CoreLib "core-macros.visp"; VispFile.CoreLib "core.visp" ]

let mainFile = [ VispFile.Main filePath ]

let knownArguments = [ "--no-lib"; "--release"; "--package" ] |> Set.ofList
Expand All @@ -24,7 +22,7 @@ let main args =
if (Array.contains "--no-lib" args) then
mainFile
else
coreLibs @ mainFile
CoreLibs @ mainFile

let release =
if Array.contains "--release" args then
Expand Down
33 changes: 21 additions & 12 deletions src/Visp.Compiler/CoreParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ module CoreParser =

Path.Combine(src_dir, "..", "..", "visp", "lib", name) |> Path.GetFullPath

let private tfs =
[| Transforms.SyntaxMacros.expand
Transforms.QuasiquoteExpander.expand
Transforms.BuiltinMacros.expand
Transforms.Common.transformLambdaShortHands |]

let expandExpr expr =
Transforms.Helpers.runTransforms tfs expr

let transformFile file =
Transforms.Helpers.transformParsedFile expandExpr file

let writeParsedFile file outputStream (template: string) =
let fileWriter = Writer.CustomFileWriter(outputStream, 2, "//")
fileWriter.Write(template.Trim())
Expand All @@ -26,7 +38,7 @@ module CoreParser =
Visp.Syntax.SynWriter.Write.writeParsedFile writer file
fileWriter.WriteLine()

let writeToStreamNew file outputStream filePath =
let writeToStreamNew file outputStream _ =
let fileWriter = Writer.CustomFileWriter(outputStream, 2, "//")

let mainProgram =
Expand Down Expand Up @@ -63,7 +75,7 @@ let state = { Todo = () }
eprintfn "Message: %A" ctx.Message
| _ -> ()

let private mkTokenizer () =
let private mkTokenizerWithArgs args =
let tokens args buf =
let next =
match args.mode with
Expand All @@ -83,6 +95,8 @@ let state = { Todo = () }
macroTable.AddMacroName s
()
| MACRO_NAME _ -> args.Nested <| LexMode.TokenStream TokenStreamMode.Macro
| HASH_PAREN
| HASH_BRACKET
| LPAREN
| LBRACE
| LBRACKET
Expand All @@ -94,7 +108,10 @@ let state = { Todo = () }

next

tokens <| mkDefaultLextArgs ()
tokens args

let private mkTokenizer () =
mkTokenizerWithArgs <| mkDefaultLextArgs ()

let parseFile filePath returnLast =
let (stream, reader, lexbuf) = UnicodeFileAsLexbuf(filePath, None)
Expand Down Expand Up @@ -123,19 +140,11 @@ let state = { Todo = () }

reraise ()

let getTokenizer str fileName =
let lexbuf = LexBuffer<_>.FromString str
lexbuf.EndPos <- Position.FirstLine fileName

let tokenizer = mkTokenizer ()

(fun () -> tokenizer lexbuf)

let getTokens str fileName =
let lexbuf = LexBuffer<_>.FromString str
lexbuf.EndPos <- Position.FirstLine fileName

let tokenizer = mkTokenizer ()
let tokenizer = mkTokenizerWithArgs <| mkTokenStreamArgs ()

seq {
while not lexbuf.IsPastEndOfStream do
Expand Down
11 changes: 8 additions & 3 deletions src/Visp.Compiler/FsharpGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ let tempDirPath name =

Path.Combine(src_dir, "..", "..", ".tmp", name) |> Path.GetFullPath

let coreLibPath name =
let CoreLibRoot () =
match Environment.GetEnvironmentVariable("VISP_FS_LIB_PATH") with
| null ->
let src_dir = __SOURCE_DIRECTORY__
Path.Combine(src_dir, "..", "..", "visp", "lib", name) |> Path.GetFullPath
| path -> Path.Combine(path, name) |> Path.GetFullPath
Path.Combine(src_dir, "..", "..", "visp", "lib") |> Path.GetFullPath
| path -> path |> Path.GetFullPath

let coreLibPath name =
Path.Combine(CoreLibRoot(), name) |> Path.GetFullPath

let runtimeLibPath =
let src_dir = __SOURCE_DIRECTORY__
Expand Down Expand Up @@ -236,6 +239,8 @@ let ARGV = System.Environment.GetCommandLineArgs()
let state = {{ Todo = () }}
"""

let CoreLibs = [ VispFile.CoreLib "core-macros.visp"; VispFile.CoreLib "core.visp" ]

type FsharpGenerator(fs: IFileSystem, dir: string) =
member _.fs = fs
member _.dir = dir
Expand Down
34 changes: 28 additions & 6 deletions src/Visp.Compiler/Lexer.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ let newline (lexbuf: LexBuffer<_>) =
let outputToken lexbuf =
(sprintf "token: '%s' Line: %d Column: %d" (lexeme lexbuf) (lexbuf.StartPos.Line+1) lexbuf.StartPos.Column)

let unexpected_char mode lexbuf =
failwith (sprintf "%s SyntaxError: Unexpected char: '%s' Line: %d Column: %d" mode (lexeme lexbuf) (lexbuf.StartPos.Line+1) lexbuf.StartPos.Column)
let unexpected_char mode (lexbuf: LexBuffer<_>) =
let file = lexbuf.EndPos.FileName
failwith (sprintf "%s SyntaxError: Unexpected char: '%s' %s:line %d Column: %d" mode (lexeme lexbuf) file (lexbuf.StartPos.Line+1) lexbuf.StartPos.Column)

let startString (lexbuf: LexBuffer<_>) =
let buf = StringBuffer()
Expand Down Expand Up @@ -54,7 +55,7 @@ let exp = ['e' 'E'] ['-' '+']? digit+
let decimal = '-'? digit* frac? exp?
let letter = ['A'-'Z'] | ['a'-'z']

let SymbolicStartCharacters = ['%' '+' '-' '!' '?' '_' '-' '*' '=' '<' '>' ]
let SymbolicStartCharacters = ['%' '+' '-' '!' '?' '_' '-' '*' '=' '<' '>' '&' ]
let SymbolicExtra = [ '.' '\'' '/' ]

let ident_start_char = letter | SymbolicStartCharacters
Expand Down Expand Up @@ -103,6 +104,7 @@ rule token (args: LexArgs) (skip: bool) = parse
| ']' { RBRACKET }
| ':' { COLON }
| ',' { COMMA }
| '|' { BAR }

| "..." { SYMBOL (lexeme lexbuf) }

Expand All @@ -113,12 +115,14 @@ rule token (args: LexArgs) (skip: bool) = parse
| '.' anyspace+ { DOT }
| '+' propShort { PROP_PLUS (lexeme lexbuf) }
| "#(" { HASH_PAREN }
| "#[" { HASH_BRACKET }
| "#{" { HASH_BRACE }
| ".." { DOTDOT }
| "'" { QUOTE_SYM }
| ".[" { DOT_BRACKET }
| ".+" { DOT_PLUS }
| '.' letter+ { DOT_METHOD (lexeme lexbuf) }
| "#{" { HASH_BRACE }
| '.' propShort { DOT_METHOD (lexeme lexbuf) }
| '-' propShort { APPLY_METHOD (lexeme lexbuf) }
| '@' { AT }
| "#\\" charWords { CHAR (lexeme lexbuf) }
| "#\\u" unicodeChars { CHAR (lexeme lexbuf) }
Expand Down Expand Up @@ -170,7 +174,6 @@ rule token (args: LexArgs) (skip: bool) = parse
let text = lexeme lexbuf
symbolOrKeyword text
}
| ',' { COMMA }
| _ { unexpected_char "token" lexbuf }

and tokenStream (args: LexArgs) (skip: bool) = parse
Expand All @@ -190,10 +193,20 @@ and tokenStream (args: LexArgs) (skip: bool) = parse
| ']' { RBRACKET }
| ':' anyspace+ { COLON }
| ',' { COMMA }
| '|' { BAR }
| '.' anyspace+ { DOT }
| "#(" { HASH_PAREN }
| "#[" { HASH_BRACKET }
| "#{" { HASH_BRACE }

| "..." { SYMBOL (lexeme lexbuf) }

// operators
| '-' anyspace+ { SYMBOL "-" }
| '*' anyspace+ { SYMBOL "*" }
| '+' anyspace+ { SYMBOL "+" }
| '/' anyspace+ { SYMBOL "/" }

// Constants
| "#()" { UNIT }
| "()" { UNIT }
Expand Down Expand Up @@ -231,6 +244,15 @@ and tokenStream (args: LexArgs) (skip: bool) = parse

// Exprs
| ':' ident_char+ { KEYWORD (lexeme lexbuf) }
| '.' ident_char+ {
let text = lexeme lexbuf;
SYMBOL text }
| '+' ident_char+ {
let text = lexeme lexbuf;
SYMBOL text }
| '-' ident_char+ {
let text = lexeme lexbuf;
SYMBOL text }
| ident {
let text = lexeme lexbuf
if args.mode.IsQuasiquoteMode then
Expand Down
Loading