Skip to content

Commit c6e3c64

Browse files
committed
Reimplement string literals
1 parent 257afe8 commit c6e3c64

File tree

7 files changed

+70
-29
lines changed

7 files changed

+70
-29
lines changed

examples/syntax.sg

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,49 @@
1-
'static definition of constellation
2-
(:= x [
3-
[+a]
1+
'define ray
2+
(:= a (-f X))
3+
4+
'define star
5+
(:= b [(-f X)])
6+
7+
'define constellation
8+
(:= c [
9+
@[+a] 'focus
410
[-a b]])
511

6-
(:= y #x)
12+
'full focus
13+
(show @[ [a] [b] [c] ])
14+
15+
'identifier
16+
(:= x #a)
717

8-
(:= z (-f X))
18+
'union
19+
(:= x (union #a #b))
920

1021
'string literals
11-
'(:= w ["hello world"])
22+
(:= s ["hello world"])
1223

1324
'cons
14-
(:= w [(+w [0 1 0 1 e])])
25+
' [0 1 e] == %cons(0 (%cons 1 %nil))
26+
(:= w (+w [0 1 0 1]))
27+
28+
'stack
29+
' <s s 0> == (s (s 0))
30+
(:= n (+nat <s s 0>))
1531

16-
'print result of execution
32+
'execution
1733
(:= x [(+f X) X])
1834
(:= y (-f a))
35+
(:= ex (exec (union #x #y))) 'non-linear
36+
(:= ex (linexec (union #x #y))) 'linear
37+
38+
'show constellation
1939
<show exec (union @#x #y)>
40+
(show [ [a] [b] [c] ])
41+
(show #s)
2042

2143
'complex identifiers
2244
(:= (f a b) [(function a b)])
2345
(show #(f a b))
2446

25-
'show (literal) contellations
26-
(show [ [a] [b] [c] ])
27-
28-
'full focus
29-
(show @[ [a] [b] [c] ])
30-
3147
'inequality constraints
3248
(:= ineq [
3349
[(+f a)]
@@ -39,7 +55,7 @@
3955
'interactive debugging of execution
4056
'(trace #ineq)
4157

42-
'dynamic definition of constellation
58+
'process
4359
(:= c (process
4460
(+n0 0) 'base constellation
4561
[(-n0 X) (+n1 (s X))] 'interacts with previous
@@ -77,15 +93,16 @@
7793
' [#1=>[(+f X) X)]]
7894
' [#2=>(-f a)]>
7995

80-
'checkers & typechecking
81-
(:= checker [
82-
[+interaction (union @#tested #test)]
83-
[+expect ok]])
84-
96+
'define type
8597
(spec nat [
8698
[(-nat 0) ok]
8799
[(-nat (s N)) (+nat N)]])
88100

101+
'expect
102+
(:= x 0)
103+
(== x 0)
104+
'(== x 1)
105+
89106
'manual type checking
90107
(:= 0 (+nat 0))
91108
(:= test @(exec (union @#0 #nat)))

nvim/syntax/stellogen.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
syn clear
22

3-
syn keyword sgKeyword kill clean eval show use exec spec linear trace process run union
3+
syn keyword sgKeyword kill clean eval show use exec spec linexec trace process run union
44
syn match sgComment "\s*'[^'].*$"
55
syn match sgId "#\%(\l\|\d\)\w*"
66
syn region sgComment start="'''" end="'''" contains=NONE

src/expr.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Raw = struct
88
type t =
99
| Symbol of string
1010
| Var of ident
11+
| String of string
1112
| Focus of t
1213
| Unquote of t
1314
| List of t list
@@ -32,6 +33,8 @@ let unquote_op = "#"
3233

3334
let focus_op = "@"
3435

36+
let string_op = primitive "string"
37+
3538
let def_op = ":="
3639

3740
let expect_op = "=="
@@ -55,6 +58,7 @@ let rec to_string : expr -> string = function
5558
let rec expand_macro : Raw.t -> expr = function
5659
| Raw.Symbol s -> Symbol s
5760
| Raw.Var x -> Var x
61+
| Raw.String s -> List [ Symbol string_op; Symbol s ]
5862
| Raw.Unquote e' -> Unquote (expand_macro e')
5963
| Raw.Focus e' -> List [ Symbol focus_op; expand_macro e' ]
6064
| Raw.List es -> List (List.map ~f:expand_macro es)

src/lexer.ml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,34 @@ and read lexbuf =
4040
| '|' -> BAR
4141
| '\'' -> comment lexbuf
4242
| "'''" -> comments lexbuf
43-
| '"' -> STRMARK
43+
| '"' -> string_literal lexbuf
4444
| space | newline -> read lexbuf
4545
| eof -> EOF
4646
| _ ->
4747
raise
4848
(SyntaxError
4949
("Unexpected character '" ^ Utf8.lexeme lexbuf ^ "' during lexing") )
50+
51+
and string_literal lexbuf =
52+
let buffer = Buffer.create 32 in
53+
let rec loop () =
54+
match%sedlex lexbuf with
55+
| '"' -> STRING (Buffer.contents buffer)
56+
| '\\', any ->
57+
let escaped =
58+
match%sedlex lexbuf with
59+
| 'n' -> '\n'
60+
| 't' -> '\t'
61+
| '\\' -> '\\'
62+
| '"' -> '"'
63+
| _ -> failwith "Unknown escape sequence"
64+
in
65+
Buffer.add_char buffer escaped;
66+
loop ()
67+
| eof -> failwith "Unterminated string literal"
68+
| any ->
69+
Buffer.add_string buffer (Sedlexing.Utf8.lexeme lexbuf);
70+
loop ()
71+
| _ -> failwith "Invalid character in string literal"
72+
in
73+
loop ()

src/parser.mly

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ open Expr.Raw
44

55
%token <string> VAR
66
%token <string> SYM
7-
%token STRMARK
7+
%token <string> STRING
88
%token AT
99
%token BAR
1010
%token LPAR RPAR
@@ -39,6 +39,7 @@ let params :=
3939
let expr :=
4040
| ~=SYM; <Symbol>
4141
| ~=VAR; <Var>
42+
| ~=STRING; <String>
4243
| UNQUOTE; ~=expr; <Unquote>
4344
| AT; ~=expr; <Focus>
4445
| ~=pars(expr+); <List>

src/sgen_ast.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,10 @@ and substitution =
2727
| SGal of ident * sgen_expr
2828

2929
type err =
30-
| ReservedWord of string
3130
| ExpectError of marked_constellation * marked_constellation * ident
3231
| UnknownID of string
3332

34-
type env =
35-
{ objs : (ident * sgen_expr) list }
33+
type env = { objs : (ident * sgen_expr) list }
3634

3735
let initial_env = { objs = [] }
3836

src/sgen_eval.ml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,6 @@ let pp_err e : (string, err) Result.t =
9595
let open Lsc_ast.StellarRays in
9696
let open Printf in
9797
match e with
98-
| ReservedWord x ->
99-
sprintf "%s: identifier '%s' is reserved.\n" (red "ReservedWord Error") x
100-
|> Result.return
10198
| ExpectError (x, e, Func ((Null, f), [])) when equal_string f "default" ->
10299
sprintf "%s:\n* expected: %s\n* got: %s\n" (red "Expect Error")
103100
(x |> remove_mark_all |> string_of_constellation)

0 commit comments

Comments
 (0)