diff options
author | William Carroll <wpcarro@gmail.com> | 2022-10-24T21·53-0400 |
---|---|---|
committer | wpcarro <wpcarro@gmail.com> | 2022-10-25T03·59+0000 |
commit | ee235235b98d01b00de5a446b48d6dec574b1458 (patch) | |
tree | 21500ed0dbe683de23cea8a1928604d433743595 /users/wpcarro/scratch/compiler/expr_parser.ml | |
parent | 1e9c3955bf2c17206c6dd536ebaf96a7e6f4f22d (diff) |
feat(wpcarro/compiler): Support string literal type r/5196
``` repl> "Hello, world" String repl> (fn x "testing") a0 -> String ``` Change-Id: Ia76299a56aa4d2032c9a21277e2fddfb2e055831 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7079 Tested-by: BuildkiteCI Reviewed-by: wpcarro <wpcarro@gmail.com>
Diffstat (limited to 'users/wpcarro/scratch/compiler/expr_parser.ml')
-rw-r--r-- | users/wpcarro/scratch/compiler/expr_parser.ml | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/users/wpcarro/scratch/compiler/expr_parser.ml b/users/wpcarro/scratch/compiler/expr_parser.ml index 04ec1dbe928d..8ac6756a4af3 100644 --- a/users/wpcarro/scratch/compiler/expr_parser.ml +++ b/users/wpcarro/scratch/compiler/expr_parser.ml @@ -5,6 +5,7 @@ * * Helpers: * symbol -> [-a-z]+ + * string -> '"' [^"]* '"' * boolean -> 'true' | 'false' * integer -> [1-9][0-9]* * @@ -13,7 +14,7 @@ * binding -> '(' 'let' symbol expr expr ')' * funcdef -> '(' 'fn' symbol expr ')' * funccall -> '(' ( symbol | funcdef) expr ')' - * literal -> boolean | integer + * literal -> string | boolean | integer * variable -> symbol * * Example Usage: @@ -52,6 +53,17 @@ let tokenize (x : string) : token array = while !i < String.length x do match x.[!i] with | ' ' -> i := !i + 1 + (* strings *) + | '"' -> + let curr = ref "\"" in + i := !i + 1; + while x.[!i] != '"' do + curr := !curr ^ "?"; + i := !i + 1 + done; + curr := !curr ^ "\""; + Queue.push !curr q; + i := !i + 1 | '(' -> Queue.push "(" q; i := !i + 1 @@ -95,7 +107,14 @@ let parse_literal (p : parser) : Types.value option = | Some n -> p#advance; Some (ValueLiteral (LiteralInt n)) - | _ -> parse_variable p) + | _ -> + if String.starts_with "\"" x then + begin + p#advance; + Some (ValueLiteral (LiteralString x)) + end + else + parse_variable p) | _ -> None let rec parse_expression (p : parser) : Types.value option = @@ -144,7 +163,8 @@ let print_tokens (xs : string array) = |> List.map (Printf.sprintf "\"%s\"") |> String.concat ", " |> Printf.sprintf "tokens: [ %s ]" - |> print_string |> print_newline + |> print_string + |> print_newline let parse_language (x : string) : Types.value option = let tokens = tokenize x in |