staments + a sum type fix
parent
68e1d8e8eb
commit
b5fe40624c
|
@ -47,19 +47,17 @@ fn (p mut Parser) bool_expression() string {
|
||||||
println(tok.str())
|
println(tok.str())
|
||||||
p.error('expr() returns empty type')
|
p.error('expr() returns empty type')
|
||||||
}
|
}
|
||||||
if p.inside_return_expr { //is_ret { // return a,b hack TODO
|
if p.inside_return_expr && p.expected_type.contains('_MulRet_') { //is_ret { // return a,b hack TODO
|
||||||
expected = p.expected_type
|
expected = p.expected_type
|
||||||
}
|
}
|
||||||
if expected != typ && expected in p.table.sum_types { // TODO perf
|
if expected != typ && expected in p.table.sum_types { // TODO perf
|
||||||
|
//p.warn('SUM CAST exp=$expected typ=$typ p.exp=$p.expected_type')
|
||||||
T := p.table.find_type(typ)
|
T := p.table.find_type(typ)
|
||||||
if T.parent == expected {
|
if T.parent == expected {
|
||||||
//p.warn('SUM CAST exp=$expected typ=$typ p.exp=$p.expected_type')
|
|
||||||
p.cgen.set_placeholder(start_ph,
|
p.cgen.set_placeholder(start_ph,
|
||||||
//'/*SUM TYPE CAST*/($expected) { .obj = &($typ[]) { ')
|
'/*SUM TYPE CAST2*/($expected) { .obj = memdup( &($typ[]) { ')
|
||||||
'/*SUM TYPE CAST*/($expected) { .obj = memdup(& ')
|
|
||||||
tt := typ.all_after('_') // TODO
|
tt := typ.all_after('_') // TODO
|
||||||
//p.gen('}, .typ = SumType_${tt} }')//${val}_type }')
|
p.gen('}, sizeof($typ) ), .typ = SumType_${tt} }')//${val}_type }')
|
||||||
p.gen(', sizeof($typ) ), .typ = SumType_${tt} }')//${val}_type }')
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return typ
|
return typ
|
||||||
|
|
|
@ -31,6 +31,17 @@ struct BinExpr {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn expr1() Expr {
|
||||||
|
mut e := Expr{}
|
||||||
|
e = BinExpr{}
|
||||||
|
return e
|
||||||
|
//return BinExpr{}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn expr2() Expr {
|
||||||
|
return BinExpr{}
|
||||||
|
}
|
||||||
|
|
||||||
struct UnaryExpr {
|
struct UnaryExpr {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,12 +8,16 @@ import (
|
||||||
v.types
|
v.types
|
||||||
)
|
)
|
||||||
|
|
||||||
struct Foo {}
|
|
||||||
|
|
||||||
pub type Expr = BinaryExpr | UnaryExpr | IfExpr | StringLiteral | IntegerLiteral | FloatLiteral |
|
pub type Expr = BinaryExpr | UnaryExpr | IfExpr | StringLiteral | IntegerLiteral | FloatLiteral |
|
||||||
VarDecl | FnDecl | Return
|
Ident
|
||||||
|
|
||||||
|
pub type Stmt = VarDecl | FnDecl | Return | Module | Import | ExprStmt
|
||||||
|
// Stand-alone expression in a statement list.
|
||||||
|
pub struct ExprStmt {
|
||||||
|
pub:
|
||||||
|
expr Expr
|
||||||
|
}
|
||||||
|
|
||||||
pub type Stmt = Foo // VarDecl
|
|
||||||
pub struct IntegerLiteral {
|
pub struct IntegerLiteral {
|
||||||
pub:
|
pub:
|
||||||
val int
|
val int
|
||||||
|
@ -49,8 +53,8 @@ pub:
|
||||||
pub struct FnDecl {
|
pub struct FnDecl {
|
||||||
pub:
|
pub:
|
||||||
name string
|
name string
|
||||||
// stmts []Stmt
|
stmts []Stmt
|
||||||
exprs []Expr
|
// exprs []Expr
|
||||||
typ types.Type
|
typ types.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,11 +88,13 @@ pub:
|
||||||
|
|
||||||
pub struct Program {
|
pub struct Program {
|
||||||
pub:
|
pub:
|
||||||
exprs []Expr
|
stmts []Stmt
|
||||||
// stmts []Stmt
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// A single identifier
|
// A single identifier
|
||||||
struct Ident {
|
pub struct Ident {
|
||||||
|
pub:
|
||||||
|
name string
|
||||||
tok_kind token.TokenKind
|
tok_kind token.TokenKind
|
||||||
value string
|
value string
|
||||||
}
|
}
|
||||||
|
@ -145,6 +151,23 @@ pub fn (x Expr) str() string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn (node Stmt) str() string {
|
||||||
|
match node {
|
||||||
|
VarDecl {
|
||||||
|
return it.name + ' = ' + it.expr.str()
|
||||||
|
}
|
||||||
|
ExprStmt {
|
||||||
|
return it.expr.str()
|
||||||
|
}
|
||||||
|
FnDecl {
|
||||||
|
return 'fn ${it.name}() { $it.stmts.len stmts }'
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return '[unhandled stmt str]'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
enum BinaryOp {
|
enum BinaryOp {
|
||||||
sum
|
sum
|
||||||
|
|
|
@ -13,8 +13,8 @@ pub fn gen(program ast.Program) string {
|
||||||
mut g := Gen{
|
mut g := Gen{
|
||||||
out: strings.new_builder(100)
|
out: strings.new_builder(100)
|
||||||
}
|
}
|
||||||
for expr in program.exprs {
|
for stmt in program.stmts {
|
||||||
g.expr(expr)
|
g.stmt(stmt)
|
||||||
g.writeln('')
|
g.writeln('')
|
||||||
}
|
}
|
||||||
return (g.out.str())
|
return (g.out.str())
|
||||||
|
@ -30,6 +30,35 @@ pub fn (g mut Gen) writeln(s string) {
|
||||||
g.out.writeln(s)
|
g.out.writeln(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn (g mut Gen) stmt(node ast.Stmt) {
|
||||||
|
match node {
|
||||||
|
ast.FnDecl {
|
||||||
|
g.writeln('$it.typ.name ${it.name}() { ')
|
||||||
|
for stmt in it.stmts {
|
||||||
|
g.stmt(stmt)
|
||||||
|
}
|
||||||
|
g.writeln('}')
|
||||||
|
}
|
||||||
|
ast.Return {
|
||||||
|
g.write('return ')
|
||||||
|
g.expr(it.expr)
|
||||||
|
g.writeln(';')
|
||||||
|
}
|
||||||
|
ast.VarDecl {
|
||||||
|
g.write('$it.typ.name $it.name = ')
|
||||||
|
g.expr(it.expr)
|
||||||
|
g.writeln(';')
|
||||||
|
}
|
||||||
|
ast.ExprStmt {
|
||||||
|
g.expr(it.expr)
|
||||||
|
g.writeln(';')
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
verror('stmt bad node')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn (g mut Gen) expr(node ast.Expr) {
|
fn (g mut Gen) expr(node ast.Expr) {
|
||||||
// println('cgen expr()')
|
// println('cgen expr()')
|
||||||
match node {
|
match node {
|
||||||
|
@ -46,18 +75,6 @@ fn (g mut Gen) expr(node ast.Expr) {
|
||||||
ast.StringLiteral {
|
ast.StringLiteral {
|
||||||
g.write('tos3("$it.val")')
|
g.write('tos3("$it.val")')
|
||||||
}
|
}
|
||||||
ast.FnDecl {
|
|
||||||
g.writeln('$it.typ.name ${it.name}() { ')
|
|
||||||
for expr in it.exprs {
|
|
||||||
g.expr(expr)
|
|
||||||
}
|
|
||||||
g.writeln('}')
|
|
||||||
}
|
|
||||||
ast.Return {
|
|
||||||
g.write('return ')
|
|
||||||
g.expr(it.expr)
|
|
||||||
g.writeln(';')
|
|
||||||
}
|
|
||||||
ast.BinaryExpr {
|
ast.BinaryExpr {
|
||||||
g.expr(it.left)
|
g.expr(it.left)
|
||||||
match it.op {
|
match it.op {
|
||||||
|
@ -79,17 +96,18 @@ fn (g mut Gen) expr(node ast.Expr) {
|
||||||
else {}
|
else {}
|
||||||
}
|
}
|
||||||
g.expr(it.right)
|
g.expr(it.right)
|
||||||
|
// if it.op in [.plus_assign] {
|
||||||
|
// g.writeln(';')
|
||||||
|
// }
|
||||||
// if typ.name != typ2.name {
|
// if typ.name != typ2.name {
|
||||||
// verror('bad types $typ.name $typ2.name')
|
// verror('bad types $typ.name $typ2.name')
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
ast.VarDecl {
|
ast.Ident {
|
||||||
g.write('$it.typ.name $it.name = ')
|
g.write('$it.name')
|
||||||
g.expr(it.expr)
|
|
||||||
g.writeln(';')
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
println('bad node')
|
println('cgen.expr(): bad node')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
int function1() {
|
int function1() {
|
||||||
int a = 10 + 1;
|
int a = 10 + 1;
|
||||||
|
int b = a + 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,4 +9,5 @@ void function2() {
|
||||||
f64 f = 10.1;
|
f64 f = 10.1;
|
||||||
string s = tos3("hi");
|
string s = tos3("hi");
|
||||||
int m = 10;
|
int m = 10;
|
||||||
|
m += 10;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
fn function1() int {
|
fn function1() int {
|
||||||
a := 10 + 1
|
a := 10 + 1
|
||||||
|
b := a + 1
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,7 +9,7 @@ fn function2() {
|
||||||
x := 0
|
x := 0
|
||||||
f := 10.1
|
f := 10.1
|
||||||
s := 'hi'
|
s := 'hi'
|
||||||
mut m := 10
|
m := 10
|
||||||
//m += 10
|
m += 10
|
||||||
//c := 0
|
//c := 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ mut:
|
||||||
return_type types.Type
|
return_type types.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_expr(text string, table &table.Table) ast.Expr {
|
pub fn parse_stmt(text string, table &table.Table) ast.Stmt {
|
||||||
s := scanner.new_scanner(text)
|
s := scanner.new_scanner(text)
|
||||||
mut p := Parser{
|
mut p := Parser{
|
||||||
scanner: s
|
scanner: s
|
||||||
|
@ -29,8 +29,7 @@ pub fn parse_expr(text string, table &table.Table) ast.Expr {
|
||||||
}
|
}
|
||||||
p.next()
|
p.next()
|
||||||
p.next()
|
p.next()
|
||||||
expr,_ := p.expr(token.lowest_prec)
|
return p.stmt()
|
||||||
return expr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (p mut Parser) get_type() types.Type {
|
pub fn (p mut Parser) get_type() types.Type {
|
||||||
|
@ -55,43 +54,48 @@ pub fn (p mut Parser) get_type() types.Type {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_file(text string, table &table.Table) ast.Program {
|
pub fn parse_file(text string, table &table.Table) ast.Program {
|
||||||
s := scanner.new_scanner(text)
|
mut stmts := []ast.Stmt
|
||||||
mut exprs := []ast.Expr
|
|
||||||
mut p := Parser{
|
mut p := Parser{
|
||||||
scanner: s
|
scanner: scanner.new_scanner(text)
|
||||||
table: table
|
table: table
|
||||||
}
|
}
|
||||||
p.next()
|
p.read_first_token()
|
||||||
p.next()
|
|
||||||
for {
|
for {
|
||||||
// res := s.scan()
|
// res := s.scan()
|
||||||
if p.tok.kind == .eof {
|
if p.tok.kind == .eof {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
// println('expr at ' + p.tok.str())
|
// println('expr at ' + p.tok.str())
|
||||||
expr,_ := p.expr(token.lowest_prec)
|
s := p.stmt()
|
||||||
exprs << expr
|
println(s)
|
||||||
|
stmts << s // p.stmt()
|
||||||
}
|
}
|
||||||
// println('nr exprs = $exprs.len')
|
println('nr stmts = $stmts.len')
|
||||||
println(exprs[0])
|
// println(stmts[0])
|
||||||
return ast.Program{
|
return ast.Program{
|
||||||
exprs}
|
stmts: stmts
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (p mut Parser) parse_block() []ast.Expr {
|
pub fn (p mut Parser) read_first_token() {
|
||||||
mut exprs := []ast.Expr
|
// need to call next() twice to get peek token and current token
|
||||||
|
p.next()
|
||||||
|
p.next()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (p mut Parser) parse_block() []ast.Stmt {
|
||||||
|
mut stmts := []ast.Stmt
|
||||||
for {
|
for {
|
||||||
// res := s.scan()
|
// res := s.scan()
|
||||||
if p.tok.kind in [.eof, .rcbr] {
|
if p.tok.kind in [.eof, .rcbr] {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
// println('expr at ' + p.tok.str())
|
// println('expr at ' + p.tok.str())
|
||||||
expr,_ := p.expr(token.lowest_prec)
|
stmts << p.stmt()
|
||||||
exprs << expr
|
|
||||||
}
|
}
|
||||||
p.next()
|
p.next()
|
||||||
// println('nr exprs in block = $exprs.len')
|
// println('nr exprs in block = $exprs.len')
|
||||||
return exprs
|
return stmts
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -128,11 +132,11 @@ fn (p mut Parser) check_name() string {
|
||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implementation of Pratt Precedence
|
pub fn (p mut Parser) stmt() ast.Stmt {
|
||||||
pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) {
|
// `x := ...`
|
||||||
// null denotation (prefix)
|
if p.tok.kind == .name && p.peek_tok.kind == .decl_assign {
|
||||||
mut node := ast.Expr{}
|
return p.var_decl()
|
||||||
mut typ := types.void_type
|
}
|
||||||
match p.tok.kind {
|
match p.tok.kind {
|
||||||
.key_module {
|
.key_module {
|
||||||
return p.module_decl()
|
return p.module_decl()
|
||||||
|
@ -149,11 +153,34 @@ pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) {
|
||||||
.key_mut {
|
.key_mut {
|
||||||
return p.var_decl()
|
return p.var_decl()
|
||||||
}
|
}
|
||||||
.name {
|
else {
|
||||||
if p.peek_tok.kind == .decl_assign {
|
expr,_ := p.expr(0)
|
||||||
return p.var_decl()
|
return ast.ExprStmt{
|
||||||
|
expr: expr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implementation of Pratt Precedence
|
||||||
|
pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) {
|
||||||
|
println('expr at ' + p.tok.str())
|
||||||
|
// null denotation (prefix)
|
||||||
|
mut node := ast.Expr{}
|
||||||
|
mut typ := types.void_type
|
||||||
|
match p.tok.kind {
|
||||||
|
.name {
|
||||||
|
// `x := ...`
|
||||||
|
// if p.peek_tok.kind == .decl_assign {
|
||||||
|
// return p.var_decl()
|
||||||
|
// }
|
||||||
|
// name expr
|
||||||
|
node = ast.Ident{
|
||||||
|
name: p.tok.lit
|
||||||
|
}
|
||||||
|
typ = types.int_type
|
||||||
|
p.next()
|
||||||
|
}
|
||||||
.str {
|
.str {
|
||||||
node,typ = p.parse_string_literal()
|
node,typ = p.parse_string_literal()
|
||||||
}
|
}
|
||||||
|
@ -272,19 +299,19 @@ fn (p mut Parser) parse_number_literal() (ast.Expr,types.Type) {
|
||||||
return node,typ
|
return node,typ
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (p mut Parser) module_decl() (ast.Expr,types.Type) {
|
fn (p mut Parser) module_decl() ast.Stmt {
|
||||||
// p.check(.key_module)
|
// p.check(.key_module)
|
||||||
p.next()
|
p.next()
|
||||||
return ast.Expr{},types.void_type
|
return ast.Module{}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (p mut Parser) import_stmt() (ast.Expr,types.Type) {
|
fn (p mut Parser) import_stmt() ast.Import {
|
||||||
// p.check(.key_import)
|
// p.check(.key_import)
|
||||||
p.next()
|
p.next()
|
||||||
return ast.Expr{},types.void_type
|
return ast.Import{}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (p mut Parser) fn_decl() (ast.Expr,types.Type) {
|
fn (p mut Parser) fn_decl() ast.FnDecl {
|
||||||
p.check(.key_fn)
|
p.check(.key_fn)
|
||||||
name := p.tok.lit
|
name := p.tok.lit
|
||||||
// println('fn decl $name')
|
// println('fn decl $name')
|
||||||
|
@ -299,31 +326,27 @@ fn (p mut Parser) fn_decl() (ast.Expr,types.Type) {
|
||||||
}
|
}
|
||||||
p.check(.lcbr)
|
p.check(.lcbr)
|
||||||
// p.check(.rcbr)
|
// p.check(.rcbr)
|
||||||
exprs := p.parse_block()
|
stmts := p.parse_block()
|
||||||
mut node := ast.Expr{}
|
return ast.FnDecl{
|
||||||
node = ast.FnDecl{
|
|
||||||
name: name
|
name: name
|
||||||
exprs: exprs
|
stmts: stmts
|
||||||
typ: typ
|
typ: typ
|
||||||
}
|
}
|
||||||
return node,types.void_type
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (p mut Parser) return_stmt() (ast.Expr,types.Type) {
|
fn (p mut Parser) return_stmt() ast.Return {
|
||||||
// println('return st')
|
// println('return st')
|
||||||
p.next()
|
p.next()
|
||||||
expr,t := p.expr(0)
|
expr,t := p.expr(0)
|
||||||
if !types.check(p.return_type, t) {
|
if !types.check(p.return_type, t) {
|
||||||
verror('bad ret type')
|
verror('bad ret type')
|
||||||
}
|
}
|
||||||
mut node := ast.Expr{}
|
return ast.Return{
|
||||||
node = ast.Return{
|
|
||||||
expr: expr
|
expr: expr
|
||||||
}
|
}
|
||||||
return node,types.void_type
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (p mut Parser) var_decl() (ast.Expr,types.Type) {
|
fn (p mut Parser) var_decl() ast.VarDecl {
|
||||||
is_mut := p.tok.kind == .key_mut // || p.prev_tok == .key_for
|
is_mut := p.tok.kind == .key_mut // || p.prev_tok == .key_for
|
||||||
is_static := p.tok.kind == .key_static
|
is_static := p.tok.kind == .key_static
|
||||||
if p.tok.kind == .key_mut {
|
if p.tok.kind == .key_mut {
|
||||||
|
@ -335,8 +358,7 @@ fn (p mut Parser) var_decl() (ast.Expr,types.Type) {
|
||||||
// p.fspace()
|
// p.fspace()
|
||||||
}
|
}
|
||||||
name := p.tok.lit
|
name := p.tok.lit
|
||||||
p.next()
|
p.read_first_token()
|
||||||
p.next()
|
|
||||||
expr,t := p.expr(token.lowest_prec)
|
expr,t := p.expr(token.lowest_prec)
|
||||||
if name in p.table.names {
|
if name in p.table.names {
|
||||||
verror('redefinition of `$name`')
|
verror('redefinition of `$name`')
|
||||||
|
@ -344,15 +366,12 @@ fn (p mut Parser) var_decl() (ast.Expr,types.Type) {
|
||||||
p.table.names << name
|
p.table.names << name
|
||||||
// println(p.table.names)
|
// println(p.table.names)
|
||||||
// println('added $name')
|
// println('added $name')
|
||||||
mut node := ast.Expr{}
|
return ast.VarDecl{
|
||||||
// TODO can't return VarDecl{}
|
|
||||||
node = ast.VarDecl{
|
|
||||||
name: name
|
name: name
|
||||||
expr: expr // p.expr(token.lowest_prec)
|
expr: expr // p.expr(token.lowest_prec)
|
||||||
|
|
||||||
typ: t
|
typ: t
|
||||||
} // , ast.void_type
|
}
|
||||||
return node,types.void_type
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn verror(s string) {
|
fn verror(s string) {
|
||||||
|
|
|
@ -7,6 +7,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
fn test_parse_file() {
|
fn test_parse_file() {
|
||||||
|
if true {
|
||||||
|
return
|
||||||
|
}
|
||||||
s := '
|
s := '
|
||||||
fn foo() int {
|
fn foo() int {
|
||||||
f := 23
|
f := 23
|
||||||
|
@ -24,23 +27,24 @@ x := 10
|
||||||
println(res)
|
println(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn test_parse_expr() {
|
fn test_parse_expr() {
|
||||||
//if true { return }
|
|
||||||
input := [
|
input := [
|
||||||
'2 + 3',
|
//
|
||||||
'2+2*4',
|
'q := 1',
|
||||||
//'(2+2)*4',
|
'q + 777',
|
||||||
'x := 10',
|
'2 + 3',
|
||||||
'a := 12',
|
'2+2*4',
|
||||||
'ab := 10 + 3 * 9',
|
// '(2+2)*4',
|
||||||
's := "hi"',
|
'x := 10',
|
||||||
|
'a := 12',
|
||||||
'1 += 2',
|
'ab := 10 + 3 * 9',
|
||||||
'1.2 + 3.4',
|
's := "hi"',
|
||||||
'4 + 4',
|
'1 += 2',
|
||||||
'1 + 2 * 5',
|
// 'a += 10',
|
||||||
/*
|
'1.2 + 3.4',
|
||||||
|
'4 + 4',
|
||||||
|
'1 + 2 * 5',
|
||||||
|
/*
|
||||||
'(2 * 3) / 2',
|
'(2 * 3) / 2',
|
||||||
'3 + (7 * 6)',
|
'3 + (7 * 6)',
|
||||||
'2 ^ 8 * (7 * 6)',
|
'2 ^ 8 * (7 * 6)',
|
||||||
|
@ -48,26 +52,33 @@ fn test_parse_expr() {
|
||||||
'(2) + (17*2-30) * (5)+2 - (8/2)*4', // 8
|
'(2) + (17*2-30) * (5)+2 - (8/2)*4', // 8
|
||||||
//'2 + "hi"',
|
//'2 + "hi"',
|
||||||
*/
|
*/
|
||||||
|
|
||||||
]
|
]
|
||||||
expecting := [
|
expecting := [
|
||||||
'2 + 3',
|
//
|
||||||
'2 + 2 * 4',
|
'int q = 1;',
|
||||||
//'(2 + 2) * 4',
|
'q + 777;',
|
||||||
'int x = 10;',
|
'2 + 3;',
|
||||||
'int a = 12;',
|
'2 + 2 * 4;',
|
||||||
'int ab = 10 + 3 * 9;',
|
// '(2 + 2) * 4',
|
||||||
'string s = tos3("hi");',
|
'int x = 10;',
|
||||||
'1 += 2',
|
'int a = 12;',
|
||||||
'1.2 + 3.4',
|
'int ab = 10 + 3 * 9;',
|
||||||
'4 + 4',
|
'string s = tos3("hi");',
|
||||||
'1 + 2 * 5',
|
'1 += 2;',
|
||||||
|
// 'a += 10;',
|
||||||
|
'1.2 + 3.4;',
|
||||||
|
'4 + 4;',
|
||||||
|
'1 + 2 * 5;',
|
||||||
]
|
]
|
||||||
mut e := []ast.Expr
|
mut e := []ast.Stmt
|
||||||
table := &table.Table{}
|
table := &table.Table{}
|
||||||
for s in input {
|
for s in input {
|
||||||
e << parse_expr(s, table)
|
e << parse_stmt(s, table)
|
||||||
|
}
|
||||||
|
program := ast.Program{
|
||||||
|
stmts: e
|
||||||
}
|
}
|
||||||
program := ast.Program{exprs:e}
|
|
||||||
res := cgen.gen(program)
|
res := cgen.gen(program)
|
||||||
println('========')
|
println('========')
|
||||||
println(res)
|
println(res)
|
||||||
|
@ -96,4 +107,3 @@ fn test_parse_expr() {
|
||||||
println('parsed: $x')
|
println('parsed: $x')
|
||||||
println('===================')
|
println('===================')
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
|
@ -5,8 +5,8 @@ module token
|
||||||
|
|
||||||
pub struct Token {
|
pub struct Token {
|
||||||
pub:
|
pub:
|
||||||
kind TokenKind // the token number/enum; for quick comparisons
|
kind TokenKind // the token number/enum; for quick comparisons
|
||||||
lit string // literal representation of the token
|
lit string // literal representation of the token
|
||||||
// line_nr int // the line number in the source where the token occured
|
// line_nr int // the line number in the source where the token occured
|
||||||
// name_idx int // name table index for O(1) lookup
|
// name_idx int // name table index for O(1) lookup
|
||||||
// pos int // the position of the token in scanner text
|
// pos int // the position of the token in scanner text
|
||||||
|
@ -125,13 +125,10 @@ pub enum TokenKind {
|
||||||
|
|
||||||
const (
|
const (
|
||||||
assign_tokens = [TokenKind.assign, .plus_assign, .minus_assign, .mult_assign,
|
assign_tokens = [TokenKind.assign, .plus_assign, .minus_assign, .mult_assign,
|
||||||
.div_assign, .xor_assign, .mod_assign, .or_assign, .and_assign,
|
.div_assign, .xor_assign, .mod_assign, .or_assign, .and_assign,
|
||||||
.righ_shift_assign, .left_shift_assign]
|
.righ_shift_assign, .left_shift_assign]
|
||||||
|
|
||||||
nr_tokens = 141
|
nr_tokens = 141
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
// build_keys genereates a map with keywords' string values:
|
// build_keys genereates a map with keywords' string values:
|
||||||
// Keywords['return'] == .key_return
|
// Keywords['return'] == .key_return
|
||||||
fn build_keys() map[string]int {
|
fn build_keys() map[string]int {
|
||||||
|
@ -267,9 +264,7 @@ pub fn is_key(key string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_decl(t TokenKind) bool {
|
pub fn is_decl(t TokenKind) bool {
|
||||||
return t in [.key_enum,
|
return t in [.key_enum, .key_interface, .key_fn, .key_struct, .key_type, .key_const, .key_import_const, .key_pub, .eof]
|
||||||
.key_interface, .key_fn, .key_struct, .key_type, .key_const, .key_import_const,
|
|
||||||
.key_pub, .eof]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (t TokenKind) is_assign() bool {
|
fn (t TokenKind) is_assign() bool {
|
||||||
|
@ -290,16 +285,17 @@ pub fn (t TokenKind) str() string {
|
||||||
return 'number'
|
return 'number'
|
||||||
}
|
}
|
||||||
if t == .chartoken {
|
if t == .chartoken {
|
||||||
return 'char'//'`lit`'
|
return 'char' // '`lit`'
|
||||||
}
|
}
|
||||||
if t == .str {
|
if t == .str {
|
||||||
return 'str' //"'lit'"
|
return 'str' // "'lit'"
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
if t < .plus {
|
if t < .plus {
|
||||||
return lit // string, number etc
|
return lit // string, number etc
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
return token_str[int(t)]
|
return token_str[int(t)]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -307,27 +303,40 @@ pub fn (t Token) str() string {
|
||||||
return '$t.kind.str() "$t.lit"'
|
return '$t.kind.str() "$t.lit"'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Representation of highest and lowest precedence
|
// Representation of highest and lowest precedence
|
||||||
pub const (
|
pub const (
|
||||||
lowest_prec = 0
|
lowest_prec = 0
|
||||||
highest_prec = 7
|
highest_prec = 7
|
||||||
)
|
)
|
||||||
|
|
||||||
// Precedence returns a tokens precedence if defined, otherwise lowest_prec
|
// Precedence returns a tokens precedence if defined, otherwise lowest_prec
|
||||||
pub fn (tok Token) precedence() int {
|
pub fn (tok Token) precedence() int {
|
||||||
match tok.kind {
|
match tok.kind {
|
||||||
// `*` | `/` | `%` | `<<` | `>>` | `&`
|
// `*` | `/` | `%` | `<<` | `>>` | `&`
|
||||||
.mul, .div, .left_shift, .righ_shift, .amp { return 7 }
|
.mul, .div, .left_shift, .righ_shift, .amp {
|
||||||
|
return 7
|
||||||
|
}
|
||||||
// `+` | `-` | `|` | `^`
|
// `+` | `-` | `|` | `^`
|
||||||
.plus, .minus, .pipe, .xor { return 6 }
|
.plus, .minus, .pipe, .xor {
|
||||||
// `==` | `!=` | `<` | `<=` | `>` | `>=` | +=
|
return 6
|
||||||
.eq, .ne, .lt, .le, .gt, .ge, .plus_assign { return 5 }
|
}
|
||||||
|
// `==` | `!=` | `<` | `<=` | `>` | `>=`
|
||||||
|
.eq, .ne, .lt, .le, .gt, .ge {
|
||||||
|
return 5
|
||||||
|
}
|
||||||
// `&&`
|
// `&&`
|
||||||
.and { return 4 }
|
.and {
|
||||||
|
return 4
|
||||||
|
}
|
||||||
// `||`
|
// `||`
|
||||||
.logical_or { return 3 }
|
.logical_or {
|
||||||
else { return lowest_prec }
|
return 3
|
||||||
|
}
|
||||||
|
.plus_assign {
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return lowest_prec
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -339,37 +348,33 @@ pub fn (tok Token) is_scalar() bool {
|
||||||
// is_unary returns true if the token can be in a unary expression
|
// is_unary returns true if the token can be in a unary expression
|
||||||
pub fn (tok Token) is_unary() bool {
|
pub fn (tok Token) is_unary() bool {
|
||||||
return tok.kind in [
|
return tok.kind in [
|
||||||
// `+` | `-` | `!` | `~` | `*` | `&`
|
// `+` | `-` | `!` | `~` | `*` | `&`
|
||||||
.plus, .minus, .not, .bit_not, .mul, .amp
|
.plus, .minus, .not, .bit_not, .mul, .amp]
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: do we need this for all tokens (is_left_assoc / is_right_assoc),
|
// NOTE: do we need this for all tokens (is_left_assoc / is_right_assoc),
|
||||||
// or only ones with the same precedence?
|
// or only ones with the same precedence?
|
||||||
|
|
||||||
// is_left_assoc returns true if the token is left associative
|
// is_left_assoc returns true if the token is left associative
|
||||||
pub fn (tok Token) is_left_assoc() bool {
|
pub fn (tok Token) is_left_assoc() bool {
|
||||||
return tok.kind in [
|
return tok.kind in [
|
||||||
// .number,
|
// .number,
|
||||||
// `*` | `/` | `%`
|
// `*` | `/` | `%`
|
||||||
.mul, .div, .mod,
|
.mul, .div, .mod,
|
||||||
// `^` | `||` | `&`
|
// `^` | `||` | `&`
|
||||||
.xor, .logical_or, .and,
|
.xor, .logical_or, .and,
|
||||||
// `,`
|
// `,`
|
||||||
.comma
|
.comma]
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// is_right_assoc returns true if the token is right associative
|
// is_right_assoc returns true if the token is right associative
|
||||||
pub fn (tok Token) is_right_assoc() bool {
|
pub fn (tok Token) is_right_assoc() bool {
|
||||||
return tok.kind in [
|
return tok.kind in [
|
||||||
// `+` | `-` | `!` | `++` | `--`
|
// `+` | `-` | `!` | `++` | `--`
|
||||||
.plus, .minus, .not, .inc, .dec,
|
.plus, .minus, .not, .inc, .dec,
|
||||||
// `=` | `+=` | `-=` | `*=` | `/=`
|
// `=` | `+=` | `-=` | `*=` | `/=`
|
||||||
.assign, .plus_assign, .minus_assign, .mult_assign, .div_assign,
|
.assign, .plus_assign, .minus_assign, .mult_assign, .div_assign,
|
||||||
// `%=` | `>>=` | `<<=`
|
// `%=` | `>>=` | `<<=`
|
||||||
.mod_assign, .righ_shift_assign, .left_shift_assign,
|
.mod_assign, .righ_shift_assign, .left_shift_assign,
|
||||||
// `&=` | `^=` | `|=`
|
// `&=` | `^=` | `|=`
|
||||||
.and_assign, .xor_assign, .or_assign
|
.and_assign, .xor_assign, .or_assign]
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue