This commit is contained in:
Jeroen van Rijn
2019-03-17 23:27:13 +01:00
parent a6fb2dd587
commit dc706d8a6b
2 changed files with 23 additions and 25 deletions
+18 -20
View File
@@ -2,8 +2,6 @@ package cel;
import "core:fmt" import "core:fmt"
import "core:strconv" import "core:strconv"
import "core:os"
import "core:mem"
import "core:unicode/utf8" import "core:unicode/utf8"
import "core:strings" import "core:strings"
@@ -34,7 +32,7 @@ Parser :: struct {
print_value :: proc(value: Value, pretty := true, indent := 0) { print_value :: proc(value: Value, pretty := true, indent := 0) {
print_indent :: proc(indent: int) { print_indent :: proc(indent: int) {
for i in 0..indent-1 do fmt.print("\t"); for _ in 0..indent-1 do fmt.print("\t");
} }
switch v in value { switch v in value {
@@ -62,16 +60,16 @@ print_value :: proc(value: Value, pretty := true, indent := 0) {
if pretty do fmt.println(); if pretty do fmt.println();
i := 0; i := 0;
for name, value in v { for name, val in v {
if pretty { if pretty {
print_indent(indent+1); print_indent(indent+1);
fmt.printf("%s = ", name); fmt.printf("%s = ", name);
print_value(value, pretty, indent+1); print_value(val, pretty, indent+1);
fmt.println(","); fmt.println(",");
} else { } else {
if i > 0 do fmt.print(", "); if i > 0 do fmt.print(", ");
fmt.printf("%s = ", name); fmt.printf("%s = ", name);
print_value(value, pretty, indent+1); print_value(val, pretty, indent+1);
i += 1; i += 1;
} }
} }
@@ -155,7 +153,7 @@ destroy :: proc(p: ^Parser) {
delete(v); delete(v);
case Dict: case Dict:
for key, value in v do destroy_value(value); for _, dv in v do destroy_value(dv);
delete(v); delete(v);
} }
} }
@@ -201,11 +199,12 @@ unquote_char :: proc(s: string, quote: byte) -> (r: rune, multiple_bytes: bool,
} }
return -1; return -1;
} }
w: int;
if s[0] == quote && quote == '"' { if s[0] == quote && quote == '"' {
return; return;
} else if s[0] >= 0x80 { } else if s[0] >= 0x80 {
r, w := utf8.decode_rune_in_string(s); r, w = utf8.decode_rune_in_string(s);
return r, true, s[w:], true; return r, true, s[w:], true;
} else if s[0] != '\\' { } else if s[0] != '\\' {
return rune(s[0]), false, s[1:], true; return rune(s[0]), false, s[1:], true;
@@ -291,7 +290,6 @@ unquote_string :: proc(p: ^Parser, t: Token) -> (string, bool) {
return t.lit, true; return t.lit, true;
} }
s := t.lit; s := t.lit;
n := len(s);
quote := '"'; quote := '"';
if s == `""` { if s == `""` {
@@ -442,7 +440,7 @@ parse_operand :: proc(p: ^Parser) -> (Value, Pos) {
case Kind.Open_Paren: case Kind.Open_Paren:
expect_token(p, Kind.Open_Paren); expect_token(p, Kind.Open_Paren);
expr, pos := parse_expr(p); expr, _ := parse_expr(p);
expect_token(p, Kind.Close_Paren); expect_token(p, Kind.Close_Paren);
return expr, tok.pos; return expr, tok.pos;
@@ -451,7 +449,7 @@ parse_operand :: proc(p: ^Parser) -> (Value, Pos) {
elems := make([dynamic]Value, 0, 4); elems := make([dynamic]Value, 0, 4);
for p.curr_token.kind != Kind.Close_Bracket && for p.curr_token.kind != Kind.Close_Bracket &&
p.curr_token.kind != Kind.EOF { p.curr_token.kind != Kind.EOF {
elem, pos := parse_expr(p); elem, _ := parse_expr(p);
append(&elems, elem); append(&elems, elem);
if p.curr_token.kind == Kind.Semicolon && p.curr_token.lit == "\n" { if p.curr_token.kind == Kind.Semicolon && p.curr_token.lit == "\n" {
@@ -481,9 +479,9 @@ parse_operand :: proc(p: ^Parser) -> (Value, Pos) {
name, ok := unquote_string(p, name_tok); name, ok := unquote_string(p, name_tok);
if !ok do error(p, tok.pos, "Unable to unquote string"); if !ok do error(p, tok.pos, "Unable to unquote string");
expect_token(p, Kind.Assign); expect_token(p, Kind.Assign);
elem, pos := parse_expr(p); elem, _ := parse_expr(p);
if _, ok := dict[name]; ok { if _, ok2 := dict[name]; ok2 {
error(p, name_tok.pos, "Previous declaration of %s in this scope", name); error(p, name_tok.pos, "Previous declaration of %s in this scope", name);
} else { } else {
dict[name] = elem; dict[name] = elem;
@@ -533,9 +531,9 @@ parse_atom_expr :: proc(p: ^Parser, operand: Value, pos: Pos) -> (Value, Pos) {
} }
case Kind.Open_Bracket: case Kind.Open_Bracket:
open := expect_token(p, Kind.Open_Bracket); expect_token(p, Kind.Open_Bracket);
index, index_pos := parse_expr(p); index, index_pos := parse_expr(p);
close := expect_token(p, Kind.Close_Bracket); expect_token(p, Kind.Close_Bracket);
switch a in operand { switch a in operand {
@@ -613,7 +611,7 @@ parse_unary_expr :: proc(p: ^Parser) -> (Value, Pos) {
case Kind.Not: case Kind.Not:
next_token(p); next_token(p);
expr, pos := parse_unary_expr(p); expr, _ := parse_unary_expr(p);
if v, ok := expr.(bool); ok { if v, ok := expr.(bool); ok {
return !v, op.pos; return !v, op.pos;
} }
@@ -757,9 +755,9 @@ parse_binary_expr :: proc(p: ^Parser, prec_in: int) -> (Value, Pos) {
if op.kind == Kind.Question { if op.kind == Kind.Question {
cond := expr; cond := expr;
x, x_pos := parse_expr(p); x, _ := parse_expr(p);
expect_token(p, Kind.Colon); expect_token(p, Kind.Colon);
y, y_pos := parse_expr(p); y, _ := parse_expr(p);
if t, ok := cond.(bool); ok { if t, ok := cond.(bool); ok {
expr = t ? x : y; expr = t ? x : y;
@@ -824,9 +822,9 @@ parse_assignment :: proc(p: ^Parser) -> bool {
expect_token(p, Kind.Assign); expect_token(p, Kind.Assign);
name, ok := unquote_string(p, tok); name, ok := unquote_string(p, tok);
if !ok do error(p, tok.pos, "Unable to unquote string"); if !ok do error(p, tok.pos, "Unable to unquote string");
expr, pos := parse_expr(p); expr, _ := parse_expr(p);
d := top_dict(p); d := top_dict(p);
if _, ok := d[name]; ok { if _, ok2 := d[name]; ok2 {
error(p, tok.pos, "Previous declaration of %s", name); error(p, tok.pos, "Previous declaration of %s", name);
} else { } else {
d[name] = expr; d[name] = expr;
+5 -5
View File
@@ -322,7 +322,7 @@ scan_number :: proc(t: ^Tokenizer, seen_decimal_point: bool) -> (Kind, string) {
} }
if t.curr_rune == '0' { if t.curr_rune == '0' {
offset := t.offset; offset = t.offset;
advance_to_next_rune(t); advance_to_next_rune(t);
switch t.curr_rune { switch t.curr_rune {
case 'b', 'B': case 'b', 'B':
@@ -403,17 +403,17 @@ scan :: proc(t: ^Tokenizer) -> Token {
quote := r; quote := r;
tok = String; tok = String;
for { for {
r := t.curr_rune; this_r := t.curr_rune;
if r == '\n' || r < 0 { if this_r == '\n' || r < 0 {
token_error(t, "String literal not terminated"); token_error(t, "String literal not terminated");
break; break;
} }
advance_to_next_rune(t); advance_to_next_rune(t);
if r == quote { if this_r == quote {
break; break;
} }
// TODO(bill); Handle properly // TODO(bill); Handle properly
if r == '\\' && t.curr_rune == quote { if this_r == '\\' && t.curr_rune == quote {
advance_to_next_rune(t); advance_to_next_rune(t);
} }
} }