cel.odin 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. package cel;
  2. import "core:fmt"
  3. import "core:strconv"
  4. import "core:unicode/utf8"
  5. import "core:strings"
  6. Array :: []Value;
  7. Dict :: map[string]Value;
  8. Nil_Value :: struct{};
  9. Value :: union {
  10. Nil_Value,
  11. bool, i64, f64, string,
  12. Array, Dict,
  13. }
  14. Parser :: struct {
  15. tokens: [dynamic]Token,
  16. prev_token: Token,
  17. curr_token: Token,
  18. curr_token_index: int,
  19. allocated_strings: [dynamic]string,
  20. error_count: int,
  21. root: Dict,
  22. dict_stack: [dynamic]^Dict, // NOTE: Pointers may be stored on the stack
  23. }
  24. print_value :: proc(value: Value, pretty := true, indent := 0) {
  25. print_indent :: proc(indent: int) {
  26. for _ in 0..<indent {
  27. fmt.print("\t");
  28. }
  29. }
  30. switch v in value {
  31. case bool: fmt.print(v);
  32. case i64: fmt.print(v);
  33. case f64: fmt.print(v);
  34. case string: fmt.print(v);
  35. case Array:
  36. fmt.print("[");
  37. if pretty { fmt.println(); }
  38. for e, i in v {
  39. if pretty {
  40. print_indent(indent+1);
  41. print_value(e, pretty, indent+1);
  42. fmt.println(",");
  43. } else {
  44. if i > 0 { fmt.print(", "); }
  45. print_value(e);
  46. }
  47. }
  48. if pretty { print_indent(indent); }
  49. fmt.print("]");
  50. case Dict:
  51. fmt.print("{");
  52. if pretty { fmt.println(); }
  53. i := 0;
  54. for name, val in v {
  55. if pretty {
  56. print_indent(indent+1);
  57. fmt.printf("%s = ", name);
  58. print_value(val, pretty, indent+1);
  59. fmt.println(",");
  60. } else {
  61. if i > 0 { fmt.print(", "); }
  62. fmt.printf("%s = ", name);
  63. print_value(val, pretty, indent+1);
  64. i += 1;
  65. }
  66. }
  67. if pretty { print_indent(indent); }
  68. fmt.print("}");
  69. case:
  70. fmt.print("nil");
  71. case Nil_Value:
  72. fmt.print("nil");
  73. }
  74. }
  75. print :: proc(p: ^Parser, pretty := false) {
  76. for name, val in p.root {
  77. fmt.printf("%s = ", name);
  78. print_value(val, pretty);
  79. fmt.println(";");
  80. }
  81. }
  82. create_from_string :: proc(src: string) -> (^Parser, bool) {
  83. return init(transmute([]byte)src);
  84. }
  85. init :: proc(src: []byte) -> (^Parser, bool) {
  86. t: Tokenizer;
  87. tokenizer_init(&t, src);
  88. return create_from_tokenizer(&t);
  89. }
  90. create_from_tokenizer :: proc(t: ^Tokenizer) -> (^Parser, bool) {
  91. p := new(Parser);
  92. for {
  93. tok := scan(t);
  94. if tok.kind == .Illegal {
  95. return p, false;
  96. }
  97. append(&p.tokens, tok);
  98. if tok.kind == .EOF {
  99. break;
  100. }
  101. }
  102. if t.error_count > 0 {
  103. return p, false;
  104. }
  105. if len(p.tokens) == 0 {
  106. tok := Token{kind = .EOF};
  107. tok.line, tok.column = 1, 1;
  108. append(&p.tokens, tok);
  109. return p, true;
  110. }
  111. p.curr_token_index = 0;
  112. p.prev_token = p.tokens[p.curr_token_index];
  113. p.curr_token = p.tokens[p.curr_token_index];
  114. p.root = Dict{};
  115. p.dict_stack = make([dynamic]^Dict, 0, 4);
  116. append(&p.dict_stack, &p.root);
  117. for p.curr_token.kind != .EOF &&
  118. p.curr_token.kind != .Illegal &&
  119. p.curr_token_index < len(p.tokens) {
  120. if !parse_assignment(p) {
  121. break;
  122. }
  123. }
  124. return p, true;
  125. }
  126. destroy :: proc(p: ^Parser) {
  127. destroy_value :: proc(value: Value) {
  128. #partial switch v in value {
  129. case Array:
  130. for elem in v {
  131. destroy_value(elem);
  132. }
  133. delete(v);
  134. case Dict:
  135. for _, dv in v {
  136. destroy_value(dv);
  137. }
  138. delete(v);
  139. }
  140. }
  141. delete(p.tokens);
  142. for s in p.allocated_strings {
  143. delete(s);
  144. }
  145. delete(p.allocated_strings);
  146. delete(p.dict_stack);
  147. destroy_value(p.root);
  148. free(p);
  149. }
  150. error :: proc(p: ^Parser, pos: Pos, msg: string, args: ..any) {
  151. fmt.eprintf("%s(%d:%d) Error: ", pos.file, pos.line, pos.column);
  152. fmt.eprintf(msg, ..args);
  153. fmt.eprintln();
  154. p.error_count += 1;
  155. }
  156. next_token :: proc(p: ^Parser) -> Token {
  157. p.prev_token = p.curr_token;
  158. prev := p.prev_token;
  159. if p.curr_token_index+1 < len(p.tokens) {
  160. p.curr_token_index += 1;
  161. p.curr_token = p.tokens[p.curr_token_index];
  162. return prev;
  163. }
  164. p.curr_token_index = len(p.tokens);
  165. p.curr_token = p.tokens[p.curr_token_index-1];
  166. error(p, prev.pos, "Token is EOF");
  167. return prev;
  168. }
  169. unquote_char :: proc(str: string, quote: byte) -> (r: rune, multiple_bytes: bool, tail_string: string, success: bool) {
  170. hex_to_int :: proc(c: byte) -> int {
  171. switch c {
  172. case '0'..='9': return int(c-'0');
  173. case 'a'..='f': return int(c-'a')+10;
  174. case 'A'..='F': return int(c-'A')+10;
  175. }
  176. return -1;
  177. }
  178. w: int;
  179. if str[0] == quote && quote == '"' {
  180. return;
  181. } else if str[0] >= 0x80 {
  182. r, w = utf8.decode_rune_in_string(str);
  183. return r, true, str[w:], true;
  184. } else if str[0] != '\\' {
  185. return rune(str[0]), false, str[1:], true;
  186. }
  187. if len(str) <= 1 {
  188. return;
  189. }
  190. s := str;
  191. c := s[1];
  192. s = s[2:];
  193. switch c {
  194. case:
  195. return;
  196. case 'a': r = '\a';
  197. case 'b': r = '\b';
  198. case 'f': r = '\f';
  199. case 'n': r = '\n';
  200. case 'r': r = '\r';
  201. case 't': r = '\t';
  202. case 'v': r = '\v';
  203. case '\\': r = '\\';
  204. case '"': r = '"';
  205. case '\'': r = '\'';
  206. case '0'..='7':
  207. v := int(c-'0');
  208. if len(s) < 2 {
  209. return;
  210. }
  211. for i in 0..<len(s) {
  212. d := int(s[i]-'0');
  213. if d < 0 || d > 7 {
  214. return;
  215. }
  216. v = (v<<3) | d;
  217. }
  218. s = s[2:];
  219. if v > 0xff {
  220. return;
  221. }
  222. r = rune(v);
  223. case 'x', 'u', 'U':
  224. count: int;
  225. switch c {
  226. case 'x': count = 2;
  227. case 'u': count = 4;
  228. case 'U': count = 8;
  229. }
  230. if len(s) < count {
  231. return;
  232. }
  233. for i in 0..<count {
  234. d := hex_to_int(s[i]);
  235. if d < 0 {
  236. return;
  237. }
  238. r = (r<<4) | rune(d);
  239. }
  240. s = s[count:];
  241. if c == 'x' {
  242. break;
  243. }
  244. if r > utf8.MAX_RUNE {
  245. return;
  246. }
  247. multiple_bytes = true;
  248. }
  249. success = true;
  250. tail_string = s;
  251. return;
  252. }
  253. unquote_string :: proc(p: ^Parser, t: Token) -> (string, bool) {
  254. if t.kind != .String {
  255. return t.lit, true;
  256. }
  257. s := t.lit;
  258. quote := '"';
  259. if s == `""` {
  260. return "", true;
  261. }
  262. if strings.contains_rune(s, '\n') >= 0 {
  263. return s, false;
  264. }
  265. if strings.contains_rune(s, '\\') < 0 && strings.contains_rune(s, quote) < 0 {
  266. if quote == '"' {
  267. return s, true;
  268. }
  269. }
  270. buf_len := 3*len(s) / 2;
  271. buf := make([]byte, buf_len);
  272. offset := 0;
  273. for len(s) > 0 {
  274. r, multiple_bytes, tail_string, ok := unquote_char(s, byte(quote));
  275. if !ok {
  276. delete(buf);
  277. return s, false;
  278. }
  279. s = tail_string;
  280. if r < 0x80 || !multiple_bytes {
  281. buf[offset] = byte(r);
  282. offset += 1;
  283. } else {
  284. b, w := utf8.encode_rune(r);
  285. copy(buf[offset:], b[:w]);
  286. offset += w;
  287. }
  288. }
  289. new_string := string(buf[:offset]);
  290. append(&p.allocated_strings, new_string);
  291. return new_string, true;
  292. }
  293. allow_token :: proc(p: ^Parser, kind: Kind) -> bool {
  294. if p.curr_token.kind == kind {
  295. next_token(p);
  296. return true;
  297. }
  298. return false;
  299. }
  300. expect_token :: proc(p: ^Parser, kind: Kind) -> Token {
  301. prev := p.curr_token;
  302. if prev.kind != kind {
  303. got := prev.lit;
  304. if got == "\n" {
  305. got = ";";
  306. }
  307. error(p, prev.pos, "Expected %s, got %s", kind_to_string[kind], got);
  308. }
  309. next_token(p);
  310. return prev;
  311. }
  312. expect_operator :: proc(p: ^Parser) -> Token {
  313. prev := p.curr_token;
  314. if !is_operator(prev.kind) {
  315. error(p, prev.pos, "Expected an operator, got %s", prev.lit);
  316. }
  317. next_token(p);
  318. return prev;
  319. }
  320. fix_advance :: proc(p: ^Parser) {
  321. for {
  322. #partial switch t := p.curr_token; t.kind {
  323. case .EOF, .Semicolon:
  324. return;
  325. }
  326. next_token(p);
  327. }
  328. }
  329. copy_value :: proc(value: Value) -> Value {
  330. #partial switch v in value {
  331. case Array:
  332. a := make(Array, len(v));
  333. for elem, idx in v {
  334. a[idx] = copy_value(elem);
  335. }
  336. return a;
  337. case Dict:
  338. d := make(Dict, cap(v));
  339. for key, val in v {
  340. d[key] = copy_value(val);
  341. }
  342. return d;
  343. }
  344. return value;
  345. }
  346. lookup_value :: proc(p: ^Parser, name: string) -> (Value, bool) {
  347. for i := len(p.dict_stack)-1; i >= 0; i -= 1 {
  348. d := p.dict_stack[i];
  349. if val, ok := d[name]; ok {
  350. return copy_value(val), true;
  351. }
  352. }
  353. return nil, false;
  354. }
  355. parse_operand :: proc(p: ^Parser) -> (Value, Pos) {
  356. tok := p.curr_token;
  357. #partial switch p.curr_token.kind {
  358. case .Ident:
  359. next_token(p);
  360. v, ok := lookup_value(p, tok.lit);
  361. if !ok { error(p, tok.pos, "Undeclared identifier %s", tok.lit); }
  362. return v, tok.pos;
  363. case .True:
  364. next_token(p);
  365. return true, tok.pos;
  366. case .False:
  367. next_token(p);
  368. return false, tok.pos;
  369. case .Nil:
  370. next_token(p);
  371. return Nil_Value{}, tok.pos;
  372. case .Integer:
  373. next_token(p);
  374. i, _ := strconv.parse_i64(tok.lit);
  375. return i, tok.pos;
  376. case .Float:
  377. next_token(p);
  378. f, _ := strconv.parse_f64(tok.lit);
  379. return f, tok.pos;
  380. case .String:
  381. next_token(p);
  382. str, ok := unquote_string(p, tok);
  383. if !ok { error(p, tok.pos, "Unable to unquote string"); }
  384. return string(str), tok.pos;
  385. case .Open_Paren:
  386. expect_token(p, .Open_Paren);
  387. expr, _ := parse_expr(p);
  388. expect_token(p, .Close_Paren);
  389. return expr, tok.pos;
  390. case .Open_Bracket:
  391. expect_token(p, .Open_Bracket);
  392. elems := make([dynamic]Value, 0, 4);
  393. for p.curr_token.kind != .Close_Bracket &&
  394. p.curr_token.kind != .EOF {
  395. elem, _ := parse_expr(p);
  396. append(&elems, elem);
  397. if p.curr_token.kind == .Semicolon && p.curr_token.lit == "\n" {
  398. next_token(p);
  399. } else if !allow_token(p, .Comma) {
  400. break;
  401. }
  402. }
  403. expect_token(p, .Close_Bracket);
  404. return Array(elems[:]), tok.pos;
  405. case .Open_Brace:
  406. expect_token(p, .Open_Brace);
  407. dict := Dict{};
  408. append(&p.dict_stack, &dict);
  409. defer pop(&p.dict_stack);
  410. for p.curr_token.kind != .Close_Brace &&
  411. p.curr_token.kind != .EOF {
  412. name_tok := p.curr_token;
  413. if !allow_token(p, .Ident) && !allow_token(p, .String) {
  414. name_tok = expect_token(p, .Ident);
  415. }
  416. name, ok := unquote_string(p, name_tok);
  417. if !ok { error(p, tok.pos, "Unable to unquote string"); }
  418. expect_token(p, .Assign);
  419. elem, _ := parse_expr(p);
  420. if _, ok2 := dict[name]; ok2 {
  421. error(p, name_tok.pos, "Previous declaration of %s in this scope", name);
  422. } else {
  423. dict[name] = elem;
  424. }
  425. if p.curr_token.kind == .Semicolon && p.curr_token.lit == "\n" {
  426. next_token(p);
  427. } else if !allow_token(p, .Comma) {
  428. break;
  429. }
  430. }
  431. expect_token(p, .Close_Brace);
  432. return dict, tok.pos;
  433. }
  434. return nil, tok.pos;
  435. }
  436. parse_atom_expr :: proc(p: ^Parser, operand: Value, pos: Pos) -> (Value, Pos) {
  437. loop := true;
  438. for operand := operand; loop; {
  439. #partial switch p.curr_token.kind {
  440. case .Period:
  441. next_token(p);
  442. tok := next_token(p);
  443. #partial switch tok.kind {
  444. case .Ident:
  445. d, ok := operand.(Dict);
  446. if !ok || d == nil {
  447. error(p, tok.pos, "Expected a dictionary");
  448. operand = nil;
  449. continue;
  450. }
  451. name, usok := unquote_string(p, tok);
  452. if !usok { error(p, tok.pos, "Unable to unquote string"); }
  453. val, found := d[name];
  454. if !found {
  455. error(p, tok.pos, "Field %s not found in dictionary", name);
  456. operand = nil;
  457. continue;
  458. }
  459. operand = val;
  460. case:
  461. error(p, tok.pos, "Expected a selector, got %s", tok.kind);
  462. operand = nil;
  463. }
  464. case .Open_Bracket:
  465. expect_token(p, .Open_Bracket);
  466. index, index_pos := parse_expr(p);
  467. expect_token(p, .Close_Bracket);
  468. #partial switch a in operand {
  469. case Array:
  470. i, ok := index.(i64);
  471. if !ok {
  472. error(p, index_pos, "Index must be an integer for an array");
  473. operand = nil;
  474. continue;
  475. }
  476. if 0 <= i && i < i64(len(a)) {
  477. operand = a[i];
  478. } else {
  479. error(p, index_pos, "Index %d out of bounds range 0..%d", i, len(a));
  480. operand = nil;
  481. continue;
  482. }
  483. case Dict:
  484. key, ok := index.(string);
  485. if !ok {
  486. error(p, index_pos, "Index must be a string for a dictionary");
  487. operand = nil;
  488. continue;
  489. }
  490. val, found := a[key];
  491. if found {
  492. operand = val;
  493. } else {
  494. error(p, index_pos, "`%s` was not found in the dictionary", key);
  495. operand = nil;
  496. continue;
  497. }
  498. case:
  499. error(p, index_pos, "Indexing is only allowed on an array or dictionary");
  500. }
  501. case:
  502. loop = false;
  503. }
  504. }
  505. return operand, pos;
  506. }
  507. parse_unary_expr :: proc(p: ^Parser) -> (Value, Pos) {
  508. op := p.curr_token;
  509. #partial switch p.curr_token.kind {
  510. case .At:
  511. next_token(p);
  512. tok := expect_token(p, .String);
  513. v, ok := lookup_value(p, tok.lit);
  514. if !ok { error(p, tok.pos, "Undeclared identifier %s", tok.lit); }
  515. return parse_atom_expr(p, v, tok.pos);
  516. case .Add, .Sub:
  517. next_token(p);
  518. // TODO(bill): Calcuate values as you go!
  519. expr, pos := parse_unary_expr(p);
  520. #partial switch e in expr {
  521. case i64: if op.kind == .Sub { return -e, pos; }
  522. case f64: if op.kind == .Sub { return -e, pos; }
  523. case:
  524. error(p, op.pos, "Unary operator %s can only be used on integers or floats", op.lit);
  525. return nil, op.pos;
  526. }
  527. return expr, op.pos;
  528. case .Not:
  529. next_token(p);
  530. expr, _ := parse_unary_expr(p);
  531. if v, ok := expr.(bool); ok {
  532. return !v, op.pos;
  533. }
  534. error(p, op.pos, "Unary operator %s can only be used on booleans", op.lit);
  535. return nil, op.pos;
  536. }
  537. return parse_atom_expr(p, parse_operand(p));
  538. }
  539. value_order :: proc(v: Value) -> int {
  540. #partial switch _ in v {
  541. case bool, string:
  542. return 1;
  543. case i64:
  544. return 2;
  545. case f64:
  546. return 3;
  547. }
  548. return 0;
  549. }
  550. match_values :: proc(left, right: ^Value) -> bool {
  551. if value_order(right^) < value_order(left^) {
  552. return match_values(right, left);
  553. }
  554. #partial switch x in left^ {
  555. case:
  556. right^ = left^;
  557. case bool, string:
  558. return true;
  559. case i64:
  560. #partial switch y in right^ {
  561. case i64:
  562. return true;
  563. case f64:
  564. left^ = f64(x);
  565. return true;
  566. }
  567. case f64:
  568. #partial switch y in right {
  569. case f64:
  570. return true;
  571. }
  572. }
  573. return false;
  574. }
  575. calculate_binary_value :: proc(p: ^Parser, op: Kind, a_, b_: Value) -> (Value, bool) {
  576. // TODO(bill): Calculate value as you go!
  577. x, y := a_, b_;
  578. match_values(&x, &y);
  579. #partial switch a in x {
  580. case: return x, true;
  581. case bool:
  582. b, ok := y.(bool);
  583. if !ok { return nil, false; }
  584. #partial switch op {
  585. case .Eq: return a == b, true;
  586. case .NotEq: return a != b, true;
  587. case .And: return a && b, true;
  588. case .Or: return a || b, true;
  589. }
  590. case i64:
  591. b, ok := y.(i64);
  592. if !ok { return nil, false; }
  593. #partial switch op {
  594. case .Add: return a + b, true;
  595. case .Sub: return a - b, true;
  596. case .Mul: return a * b, true;
  597. case .Quo: return a / b, true;
  598. case .Rem: return a % b, true;
  599. case .Eq: return a == b, true;
  600. case .NotEq: return a != b, true;
  601. case .Lt: return a < b, true;
  602. case .Gt: return a > b, true;
  603. case .LtEq: return a <= b, true;
  604. case .GtEq: return a >= b, true;
  605. }
  606. case f64:
  607. b, ok := y.(f64);
  608. if !ok { return nil, false; }
  609. #partial switch op {
  610. case .Add: return a + b, true;
  611. case .Sub: return a - b, true;
  612. case .Mul: return a * b, true;
  613. case .Quo: return a / b, true;
  614. case .Eq: return a == b, true;
  615. case .NotEq: return a != b, true;
  616. case .Lt: return a < b, true;
  617. case .Gt: return a > b, true;
  618. case .LtEq: return a <= b, true;
  619. case .GtEq: return a >= b, true;
  620. }
  621. case string:
  622. b, ok := y.(string);
  623. if !ok { return nil, false; }
  624. #partial switch op {
  625. case .Add:
  626. n := len(a) + len(b);
  627. data := make([]byte, n);
  628. copy(data[:], a);
  629. copy(data[len(a):], b);
  630. s := string(data);
  631. append(&p.allocated_strings, s);
  632. return s, true;
  633. case .Eq: return a == b, true;
  634. case .NotEq: return a != b, true;
  635. case .Lt: return a < b, true;
  636. case .Gt: return a > b, true;
  637. case .LtEq: return a <= b, true;
  638. case .GtEq: return a >= b, true;
  639. }
  640. }
  641. return nil, false;
  642. }
  643. parse_binary_expr :: proc(p: ^Parser, prec_in: int) -> (Value, Pos) {
  644. expr, pos := parse_unary_expr(p);
  645. for prec := precedence(p.curr_token.kind); prec >= prec_in; prec -= 1 {
  646. for {
  647. op := p.curr_token;
  648. op_prec := precedence(op.kind);
  649. if op_prec != prec {
  650. break;
  651. }
  652. expect_operator(p);
  653. if op.kind == .Question {
  654. cond := expr;
  655. x, _ := parse_expr(p);
  656. expect_token(p, .Colon);
  657. y, _ := parse_expr(p);
  658. if t, ok := cond.(bool); ok {
  659. expr = t ? x : y;
  660. } else {
  661. error(p, pos, "Condition must be a boolean");
  662. }
  663. } else {
  664. right, right_pos := parse_binary_expr(p, prec+1);
  665. if right == nil {
  666. error(p, right_pos, "Expected expression on the right-hand side of the binary operator %s", op.lit);
  667. }
  668. left := expr;
  669. ok: bool;
  670. expr, ok = calculate_binary_value(p, op.kind, left, right);
  671. if !ok {
  672. error(p, pos, "Invalid binary operation");
  673. }
  674. }
  675. }
  676. }
  677. return expr, pos;
  678. }
  679. parse_expr :: proc(p: ^Parser) -> (Value, Pos) {
  680. return parse_binary_expr(p, 1);
  681. }
  682. expect_semicolon :: proc(p: ^Parser) {
  683. kind := p.curr_token.kind;
  684. #partial switch kind {
  685. case .Comma:
  686. error(p, p.curr_token.pos, "Expected ';', got ','");
  687. next_token(p);
  688. case .Semicolon:
  689. next_token(p);
  690. case .EOF:
  691. // okay
  692. case:
  693. error(p, p.curr_token.pos, "Expected ';', got %s", p.curr_token.lit);
  694. fix_advance(p);
  695. }
  696. }
  697. parse_assignment :: proc(p: ^Parser) -> bool {
  698. top_dict :: proc(p: ^Parser) -> ^Dict {
  699. assert(len(p.dict_stack) > 0);
  700. return p.dict_stack[len(p.dict_stack)-1];
  701. }
  702. if p.curr_token.kind == .Semicolon {
  703. next_token(p);
  704. return true;
  705. }
  706. if p.curr_token.kind == .EOF {
  707. return false;
  708. }
  709. tok := p.curr_token;
  710. if allow_token(p, .Ident) || allow_token(p, .String) {
  711. expect_token(p, .Assign);
  712. name, ok := unquote_string(p, tok);
  713. if !ok { error(p, tok.pos, "Unable to unquote string"); }
  714. expr, _ := parse_expr(p);
  715. d := top_dict(p);
  716. if _, ok2 := d[name]; ok2 {
  717. error(p, tok.pos, "Previous declaration of %s", name);
  718. } else {
  719. d[name] = expr;
  720. }
  721. expect_semicolon(p);
  722. return true;
  723. }
  724. error(p, tok.pos, "Expected an assignment, got %s", kind_to_string[tok.kind]);
  725. fix_advance(p);
  726. return false;
  727. }