cel.odin 17 KB

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