v2: remove expr types & some dead code from parser

pull/3931/head
Joe Conigliaro 2020-03-05 22:08:43 +11:00
parent bcaf72eccb
commit 4d55a4c097
1 changed files with 51 additions and 114 deletions

View File

@ -293,7 +293,7 @@ pub fn (p mut Parser) stmt() ast.Stmt {
match p.tok.kind { match p.tok.kind {
.key_assert { .key_assert {
p.next() p.next()
expr,_ := p.expr(0) expr := p.expr(0)
return ast.AssertStmt{ return ast.AssertStmt{
expr: expr expr: expr
} }
@ -354,12 +354,9 @@ pub fn (p mut Parser) stmt() ast.Stmt {
name: name name: name
} }
} }
// expr,typ := p.expr(0) expr := p.expr(0)
expr,_ := p.expr(0)
return ast.ExprStmt{ return ast.ExprStmt{
expr: expr expr: expr
// typ: typ
} }
} }
} }
@ -369,7 +366,7 @@ pub fn (p mut Parser) stmt() ast.Stmt {
pub fn (p mut Parser) assign_expr(left ast.Expr) ast.AssignExpr { pub fn (p mut Parser) assign_expr(left ast.Expr) ast.AssignExpr {
op := p.tok.kind op := p.tok.kind
p.next() p.next()
val,_ := p.expr(0) val := p.expr(0)
node := ast.AssignExpr{ node := ast.AssignExpr{
left: left left: left
val: val val: val
@ -392,7 +389,6 @@ fn (p mut Parser) attr() ast.Attr {
} }
/* /*
fn (p mut Parser) range_expr(low ast.Expr) ast.Expr { fn (p mut Parser) range_expr(low ast.Expr) ast.Expr {
// ,table.Type) { // ,table.Type) {
if p.tok.kind != .dotdot { if p.tok.kind != .dotdot {
@ -401,7 +397,7 @@ fn (p mut Parser) range_expr(low ast.Expr) ast.Expr {
p.check(.dotdot) p.check(.dotdot)
mut high := ast.Expr{} mut high := ast.Expr{}
if p.tok.kind != .rsbr { if p.tok.kind != .rsbr {
high,_ = p.expr(0) high = p.expr(0)
// if typ.typ.kind != .int { // if typ.typ.kind != .int {
// p.error('non-integer index `$typ.typ.name`') // p.error('non-integer index `$typ.typ.name`')
// } // }
@ -414,34 +410,6 @@ fn (p mut Parser) range_expr(low ast.Expr) ast.Expr {
} }
*/ */
/*
pub fn (p mut Parser) assign_stmt() ast.AssignStmt {
name := p.tok.lit
// println('looking for $name')
var := p.table.find_var(name) or {
p.error('assign unknown variable `$name`')
exit(1)
}
if !var.is_mut {
p.error('`$var.name` is immutable, declare it with `mut $var.name := ...`')
}
left_expr,left_type := p.expr(0)
op := p.tok.kind
// println('assignn_stmt() ' + op.str())
p.next()
right_expr,right_type := p.expr(0)
if !p.table.check(left_type, right_type) {
p.error('oops')
}
return ast.AssignStmt{
left: left_expr
right: right_expr
op: op
}
}
*/
pub fn (p &Parser) error(s string) { pub fn (p &Parser) error(s string) {
print_backtrace() print_backtrace()
mut path := p.file_name mut path := p.file_name
@ -533,7 +501,7 @@ fn (p mut Parser) struct_init() ast.StructInit {
for p.tok.kind != .rcbr { for p.tok.kind != .rcbr {
mut field_name := '' mut field_name := ''
if is_short_syntax { if is_short_syntax {
expr,_ := p.expr(0) expr := p.expr(0)
exprs << expr exprs << expr
} }
else { else {
@ -556,7 +524,7 @@ fn (p mut Parser) struct_init() ast.StructInit {
} }
if !is_short_syntax { if !is_short_syntax {
p.check(.colon) p.check(.colon)
expr,_ := p.expr(0) expr := p.expr(0)
exprs << expr exprs << expr
} }
i++ i++
@ -615,7 +583,7 @@ pub fn (p mut Parser) name_expr() ast.Expr {
to_typ := p.parse_type() to_typ := p.parse_type()
p.check(.lpar) p.check(.lpar)
mut expr := ast.Expr{} mut expr := ast.Expr{}
expr,_ = p.expr(0) expr = p.expr(0)
// TODO, string(b, len) // TODO, string(b, len)
if p.tok.kind == .comma && table.type_idx(to_typ) == table.string_type_idx { if p.tok.kind == .comma && table.type_idx(to_typ) == table.string_type_idx {
p.check(.comma) p.check(.comma)
@ -676,7 +644,7 @@ pub fn (p mut Parser) name_expr() ast.Expr {
return node return node
} }
pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) { pub fn (p mut Parser) expr(precedence int) ast.Expr {
// println('\n\nparser.expr()') // println('\n\nparser.expr()')
mut typ := table.void_type mut typ := table.void_type
mut node := ast.Expr{} mut node := ast.Expr{}
@ -686,7 +654,7 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
node = p.name_expr() node = p.name_expr()
} }
.str { .str {
node,typ = p.string_expr() node = p.string_expr()
} }
.dot { .dot {
// .enum_val // .enum_val
@ -719,11 +687,11 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
node = p.match_expr() node = p.match_expr()
} }
.number { .number {
node,typ = p.parse_number_literal() node = p.parse_number_literal()
} }
.lpar { .lpar {
p.check(.lpar) p.check(.lpar)
node,typ = p.expr(0) node = p.expr(0)
p.check(.rpar) p.check(.rpar)
node = ast.ParExpr{ node = ast.ParExpr{
expr: node expr: node
@ -765,10 +733,10 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
mut vals := []ast.Expr mut vals := []ast.Expr
for p.tok.kind != .rcbr && p.tok.kind != .eof { for p.tok.kind != .rcbr && p.tok.kind != .eof {
// p.check(.str) // p.check(.str)
key,_ := p.expr(0) key := p.expr(0)
keys << key keys << key
p.check(.colon) p.check(.colon)
val,_ := p.expr(0) val := p.expr(0)
vals << val vals << val
if p.tok.kind == .comma { if p.tok.kind == .comma {
p.next() p.next()
@ -788,7 +756,7 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
for { for {
fields << p.check_name() fields << p.check_name()
p.check(.colon) p.check(.colon)
expr,_ := p.expr(0) expr := p.expr(0)
vals << expr vals << expr
if p.tok.kind == .comma { if p.tok.kind == .comma {
p.check(.comma) p.check(.comma)
@ -834,7 +802,7 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
else if p.tok.kind == .left_shift { else if p.tok.kind == .left_shift {
tok := p.tok tok := p.tok
p.next() p.next()
right,_ := p.expr(precedence - 1) right := p.expr(precedence - 1)
node = ast.InfixExpr{ node = ast.InfixExpr{
left: node left: node
right: right right: right
@ -843,7 +811,7 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
} }
} }
else if p.tok.kind.is_infix() { else if p.tok.kind.is_infix() {
node,typ = p.infix_expr(node) node = p.infix_expr(node)
} }
// Postfix // Postfix
else if p.tok.kind in [.inc, .dec] { else if p.tok.kind in [.inc, .dec] {
@ -853,19 +821,19 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
pos: p.tok.position() pos: p.tok.position()
} }
p.next() p.next()
return node,typ return node
} }
else { else {
return node,typ return node
} }
} }
return node,typ return node
} }
fn (p mut Parser) prefix_expr() ast.PrefixExpr { fn (p mut Parser) prefix_expr() ast.PrefixExpr {
op := p.tok.kind op := p.tok.kind
p.next() p.next()
right,_ := p.expr(1) right := p.expr(1)
return ast.PrefixExpr{ return ast.PrefixExpr{
op: op op: op
right: right right: right
@ -878,7 +846,7 @@ fn (p mut Parser) index_expr(left ast.Expr) ast.IndexExpr {
if p.tok.kind == .dotdot { if p.tok.kind == .dotdot {
// [..end] // [..end]
p.next() p.next()
high,_ := p.expr(0) high := p.expr(0)
p.check(.rsbr) p.check(.rsbr)
return ast.IndexExpr{ return ast.IndexExpr{
left: left left: left
@ -889,13 +857,13 @@ fn (p mut Parser) index_expr(left ast.Expr) ast.IndexExpr {
} }
} }
} }
expr,_ := p.expr(0) // `[expr]` or `[expr..]` expr := p.expr(0) // `[expr]` or `[expr..]`
if p.tok.kind == .dotdot { if p.tok.kind == .dotdot {
// [start..end] or [start..] // [start..end] or [start..]
p.check(.dotdot) p.check(.dotdot)
mut high := ast.Expr{} mut high := ast.Expr{}
if p.tok.kind != .rsbr { if p.tok.kind != .rsbr {
high,_ = p.expr(0) high = p.expr(0)
} }
p.check(.rsbr) p.check(.rsbr)
return ast.IndexExpr{ return ast.IndexExpr{
@ -975,28 +943,24 @@ fn (p mut Parser) dot_expr(left ast.Expr, left_type table.Type) ast.Expr {
return node return node
} }
fn (p mut Parser) infix_expr(left ast.Expr) (ast.Expr,table.Type) { fn (p mut Parser) infix_expr(left ast.Expr) ast.Expr {
op := p.tok.kind op := p.tok.kind
// mut typ := p. // mut typ := p.
// println('infix op=$op.str()') // println('infix op=$op.str()')
precedence := p.tok.precedence() precedence := p.tok.precedence()
pos := p.tok.position() pos := p.tok.position()
p.next() p.next()
mut typ := table.Type{}
mut right := ast.Expr{} mut right := ast.Expr{}
right,typ = p.expr(precedence) right = p.expr(precedence)
if op.is_relational() {
typ = table.bool_type
}
mut expr := ast.Expr{} mut expr := ast.Expr{}
expr = ast.InfixExpr{ expr = ast.InfixExpr{
left: left left: left
right: right right: right
right_type: typ // right_type: typ
op: op op: op
pos: pos pos: pos
} }
return expr,typ return expr
} }
// Implementation of Pratt Precedence // Implementation of Pratt Precedence
@ -1071,12 +1035,12 @@ fn (p mut Parser) for_statement() ast.Stmt {
p.check(.semicolon) p.check(.semicolon)
if p.tok.kind != .semicolon { if p.tok.kind != .semicolon {
mut typ := table.void_type mut typ := table.void_type
cond,typ = p.expr(0) cond = p.expr(0)
} }
p.check(.semicolon) p.check(.semicolon)
if p.tok.kind != .lcbr { if p.tok.kind != .lcbr {
//inc = p.stmt() //inc = p.stmt()
inc,_ = p.expr(0) inc = p.expr(0)
} }
stmts := p.parse_block() stmts := p.parse_block()
p.close_scope() p.close_scope()
@ -1104,29 +1068,8 @@ fn (p mut Parser) for_statement() ast.Stmt {
}) })
} }
p.check(.key_in) p.check(.key_in)
mut elem_type := table.void_type
// arr_expr // arr_expr
cond,arr_typ := p.expr(0) cond := p.expr(0)
// array / map
if table.type_idx(arr_typ) == table.string_type_idx {
elem_type = table.byte_type
}
else {
arr_typ_sym := p.table.get_type_symbol(arr_typ)
match arr_typ_sym.info {
table.Array {
elem_type = it.elem_type
}
table.Map {
elem_type = it.value_type
}
else {}
// println(1)
// elem_type_sym := p.table.get_type_symbol(elem_type)
// p.error('cannot loop over type: $elem_type_sym.name')
// }
}
}
// 0 .. 10 // 0 .. 10
// start := p.tok.lit.int() // start := p.tok.lit.int()
// TODO use RangeExpr // TODO use RangeExpr
@ -1135,15 +1078,11 @@ fn (p mut Parser) for_statement() ast.Stmt {
if p.tok.kind == .dotdot { if p.tok.kind == .dotdot {
is_range = true is_range = true
p.check(.dotdot) p.check(.dotdot)
high_expr,_ = p.expr(0) high_expr = p.expr(0)
} }
// p.table.register_var(table.Var{
// name: var_name
// typ: elem_type
// })
p.scope.register_var(ast.VarDecl{ p.scope.register_var(ast.VarDecl{
name: var_name name: var_name
typ: elem_type expr: cond
}) })
stmts := p.parse_block() stmts := p.parse_block()
// println('nr stmts=$stmts.len') // println('nr stmts=$stmts.len')
@ -1159,7 +1098,7 @@ fn (p mut Parser) for_statement() ast.Stmt {
} }
} }
// `for cond {` // `for cond {`
cond,_ := p.expr(0) cond := p.expr(0)
stmts := p.parse_block() stmts := p.parse_block()
p.close_scope() p.close_scope()
return ast.ForStmt{ return ast.ForStmt{
@ -1184,7 +1123,7 @@ fn (p mut Parser) if_expr() ast.Expr {
p.open_scope() p.open_scope()
var_name := p.check_name() var_name := p.check_name()
p.check(.decl_assign) p.check(.decl_assign)
expr,typ := p.expr(0) expr := p.expr(0)
p.scope.register_var(ast.VarDecl{ p.scope.register_var(ast.VarDecl{
name: var_name name: var_name
expr: expr expr: expr
@ -1195,7 +1134,7 @@ fn (p mut Parser) if_expr() ast.Expr {
} }
} }
else { else {
cond,_ = p.expr(0) cond = p.expr(0)
} }
p.inside_if = false p.inside_if = false
mut has_else := false mut has_else := false
@ -1247,14 +1186,14 @@ fn (p mut Parser) if_expr() ast.Expr {
return node return node
} }
fn (p mut Parser) string_expr() (ast.Expr,table.Type) { fn (p mut Parser) string_expr() ast.Expr {
mut node := ast.Expr{} mut node := ast.Expr{}
node = ast.StringLiteral{ node = ast.StringLiteral{
val: p.tok.lit val: p.tok.lit
} }
if p.peek_tok.kind != .str_dollar { if p.peek_tok.kind != .str_dollar {
p.next() p.next()
return node,table.string_type return node
} }
// Handle $ interpolation // Handle $ interpolation
for p.tok.kind == .str { for p.tok.kind == .str {
@ -1279,7 +1218,7 @@ fn (p mut Parser) string_expr() (ast.Expr,table.Type) {
} }
} }
} }
return node,table.string_type return node
} }
fn (p mut Parser) array_init() ast.ArrayInit { fn (p mut Parser) array_init() ast.ArrayInit {
@ -1304,7 +1243,7 @@ fn (p mut Parser) array_init() ast.ArrayInit {
else { else {
// [1,2,3] // [1,2,3]
for i := 0; p.tok.kind != .rsbr; i++ { for i := 0; p.tok.kind != .rsbr; i++ {
expr,_ := p.expr(0) expr := p.expr(0)
exprs << expr exprs << expr
if p.tok.kind == .comma { if p.tok.kind == .comma {
p.check(.comma) p.check(.comma)
@ -1333,16 +1272,14 @@ fn (p mut Parser) array_init() ast.ArrayInit {
} }
} }
fn (p mut Parser) parse_number_literal() (ast.Expr,table.Type) { fn (p mut Parser) parse_number_literal() ast.Expr {
lit := p.tok.lit lit := p.tok.lit
mut node := ast.Expr{} mut node := ast.Expr{}
mut typ := table.int_type
if lit.contains('.') { if lit.contains('.') {
node = ast.FloatLiteral{ node = ast.FloatLiteral{
// val: lit.f64() // val: lit.f64()
val: lit val: lit
} }
typ = table.f64_type
} }
else { else {
node = ast.IntegerLiteral{ node = ast.IntegerLiteral{
@ -1350,7 +1287,7 @@ fn (p mut Parser) parse_number_literal() (ast.Expr,table.Type) {
} }
} }
p.next() p.next()
return node,typ return node
} }
fn (p mut Parser) module_decl() ast.Module { fn (p mut Parser) module_decl() ast.Module {
@ -1414,16 +1351,16 @@ fn (p mut Parser) const_decl() ast.ConstDecl {
name := p.prepend_mod(p.check_name()) name := p.prepend_mod(p.check_name())
// println('!!const: $name') // println('!!const: $name')
p.check(.assign) p.check(.assign)
expr,typ := p.expr(0) expr := p.expr(0)
fields << ast.Field{ fields << ast.Field{
name: name name: name
typ: typ // typ: typ
} }
exprs << expr exprs << expr
p.table.register_const(table.Var{ // p.table.register_const(table.Var{
name: name // name: name
typ: typ // // typ: typ
}) // })
} }
p.check(.rpar) p.check(.rpar)
return ast.ConstDecl{ return ast.ConstDecl{
@ -1543,7 +1480,7 @@ fn (p mut Parser) return_stmt() ast.Return {
} }
for { for {
// expr,ti := p.expr(0) // expr,ti := p.expr(0)
expr,_ := p.expr(0) expr := p.expr(0)
exprs << expr exprs << expr
// got_tis << ti // got_tis << ti
if p.tok.kind == .comma { if p.tok.kind == .comma {
@ -1593,7 +1530,7 @@ fn (p mut Parser) parse_assign_lhs() []ast.Ident {
fn (p mut Parser) parse_assign_rhs() []ast.Expr { fn (p mut Parser) parse_assign_rhs() []ast.Expr {
mut exprs := []ast.Expr mut exprs := []ast.Expr
for { for {
expr,_ := p.expr(0) expr := p.expr(0)
exprs << expr exprs << expr
if p.tok.kind == .comma { if p.tok.kind == .comma {
p.check(.comma) p.check(.comma)
@ -1711,7 +1648,7 @@ fn (p mut Parser) match_expr() ast.MatchExpr {
if is_mut { if is_mut {
p.next() p.next()
} }
cond,_ := p.expr(0) cond := p.expr(0)
p.check(.lcbr) p.check(.lcbr)
mut branches := []ast.MatchBranch mut branches := []ast.MatchBranch
for { for {
@ -1737,7 +1674,7 @@ fn (p mut Parser) match_expr() ast.MatchExpr {
else { else {
// Expression match // Expression match
for { for {
expr,_ := p.expr(0) expr := p.expr(0)
exprs << expr exprs << expr
if p.tok.kind != .comma { if p.tok.kind != .comma {
break break