v2: checker fixes
parent
290feaac51
commit
480af3f381
|
@ -481,15 +481,18 @@ pub fn (a []char) index(v char) int {
|
||||||
// resulting in a single output value.
|
// resulting in a single output value.
|
||||||
pub fn (a []int) reduce(iter fn(accum, curr int)int, accum_start int) int {
|
pub fn (a []int) reduce(iter fn(accum, curr int)int, accum_start int) int {
|
||||||
mut _accum := 0
|
mut _accum := 0
|
||||||
|
/*
|
||||||
_accum = accum_start
|
_accum = accum_start
|
||||||
for i := 0; i < a.len; i++ {
|
for i := 0; i < a.len; i++ {
|
||||||
_accum = iter(_accum, a[i])
|
_accum = iter(_accum, a[i])
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
return _accum
|
return _accum
|
||||||
}
|
}
|
||||||
|
|
||||||
// array_eq<T> checks if two arrays contain all the same elements in the same order.
|
// array_eq<T> checks if two arrays contain all the same elements in the same order.
|
||||||
// []int == []int (also for: i64, f32, f64, byte, string)
|
// []int == []int (also for: i64, f32, f64, byte, string)
|
||||||
|
/*
|
||||||
fn array_eq<T>(a1, a2 []T) bool {
|
fn array_eq<T>(a1, a2 []T) bool {
|
||||||
if a1.len != a2.len {
|
if a1.len != a2.len {
|
||||||
return false
|
return false
|
||||||
|
@ -521,6 +524,7 @@ pub fn (a []byte) eq(a2 []byte) bool {
|
||||||
pub fn (a []f32) eq(a2 []f32) bool {
|
pub fn (a []f32) eq(a2 []f32) bool {
|
||||||
return array_eq(a, a2)
|
return array_eq(a, a2)
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// compare_i64 for []f64 sort_with_compare()
|
// compare_i64 for []f64 sort_with_compare()
|
||||||
// sort []i64 with quicksort
|
// sort []i64 with quicksort
|
||||||
|
|
|
@ -413,6 +413,7 @@ fn sub(prev int, curr int) int {
|
||||||
return prev - curr
|
return prev - curr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
fn test_reduce() {
|
fn test_reduce() {
|
||||||
a := [1, 2, 3, 4, 5]
|
a := [1, 2, 3, 4, 5]
|
||||||
b := a.reduce(sum, 0)
|
b := a.reduce(sum, 0)
|
||||||
|
@ -427,6 +428,7 @@ fn test_reduce() {
|
||||||
assert f == -6
|
assert f == -6
|
||||||
assert g == -7
|
assert g == -7
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
fn test_filter() {
|
fn test_filter() {
|
||||||
a := [1, 2, 3, 4, 5, 6]
|
a := [1, 2, 3, 4, 5, 6]
|
||||||
|
@ -596,15 +598,15 @@ fn test_clear() {
|
||||||
fn test_trim() {
|
fn test_trim() {
|
||||||
mut arr := [1,2,3,4,5,6,7,8,9]
|
mut arr := [1,2,3,4,5,6,7,8,9]
|
||||||
assert arr.len == 9
|
assert arr.len == 9
|
||||||
|
|
||||||
arr.trim(9)
|
arr.trim(9)
|
||||||
assert arr.len == 9
|
assert arr.len == 9
|
||||||
assert arr.last() == 9
|
assert arr.last() == 9
|
||||||
|
|
||||||
arr.trim(7)
|
arr.trim(7)
|
||||||
assert arr.len == 7
|
assert arr.len == 7
|
||||||
assert arr.last() == 7
|
assert arr.last() == 7
|
||||||
|
|
||||||
arr.trim(2)
|
arr.trim(2)
|
||||||
assert arr.len == 2
|
assert arr.len == 2
|
||||||
assert arr.last() == 2
|
assert arr.last() == 2
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral |
|
pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral |
|
||||||
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
|
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
|
||||||
AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr |
|
AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr |
|
||||||
CastExpr | EnumVal | Assoc
|
CastExpr | EnumVal | Assoc | SizeOf
|
||||||
|
|
||||||
pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
|
pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
|
||||||
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
|
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
|
||||||
|
@ -145,6 +145,7 @@ mut:
|
||||||
// func Expr
|
// func Expr
|
||||||
name string
|
name string
|
||||||
args []Expr
|
args []Expr
|
||||||
|
is_c bool
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MethodCallExpr {
|
pub struct MethodCallExpr {
|
||||||
|
@ -452,6 +453,11 @@ pub:
|
||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct SizeOf {
|
||||||
|
pub:
|
||||||
|
type_name string
|
||||||
|
}
|
||||||
|
|
||||||
// string representaiton of expr
|
// string representaiton of expr
|
||||||
pub fn (x Expr) str() string {
|
pub fn (x Expr) str() string {
|
||||||
match x {
|
match x {
|
||||||
|
|
|
@ -112,7 +112,7 @@ pub fn (c mut Checker) call_expr(call_expr ast.CallExpr) table.Type {
|
||||||
fn_name := call_expr.name
|
fn_name := call_expr.name
|
||||||
if f := c.table.find_fn(fn_name) {
|
if f := c.table.find_fn(fn_name) {
|
||||||
// return_ti := f.return_ti
|
// return_ti := f.return_ti
|
||||||
if f.is_c {
|
if f.is_c || call_expr.is_c {
|
||||||
return f.return_type
|
return f.return_type
|
||||||
}
|
}
|
||||||
if call_expr.args.len < f.args.len {
|
if call_expr.args.len < f.args.len {
|
||||||
|
@ -299,12 +299,12 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type {
|
||||||
ast.AssignExpr {
|
ast.AssignExpr {
|
||||||
c.check_assign_expr(it)
|
c.check_assign_expr(it)
|
||||||
}
|
}
|
||||||
ast.IntegerLiteral {
|
|
||||||
return table.int_type
|
|
||||||
}
|
|
||||||
ast.FloatLiteral {
|
ast.FloatLiteral {
|
||||||
return table.f64_type
|
return table.f64_type
|
||||||
}
|
}
|
||||||
|
ast.IntegerLiteral {
|
||||||
|
return table.int_type
|
||||||
|
}
|
||||||
ast.PostfixExpr {
|
ast.PostfixExpr {
|
||||||
return c.postfix_expr(it)
|
return c.postfix_expr(it)
|
||||||
}
|
}
|
||||||
|
@ -314,9 +314,15 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type {
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
ast.SizeOf {
|
||||||
|
return table.int_type
|
||||||
|
}
|
||||||
ast.StringLiteral {
|
ast.StringLiteral {
|
||||||
return table.string_type
|
return table.string_type
|
||||||
}
|
}
|
||||||
|
ast.CharLiteral {
|
||||||
|
return table.byte_type
|
||||||
|
}
|
||||||
ast.PrefixExpr {
|
ast.PrefixExpr {
|
||||||
return c.expr(it.right)
|
return c.expr(it.right)
|
||||||
}
|
}
|
||||||
|
@ -433,6 +439,9 @@ pub fn (c mut Checker) ident(ident mut ast.Ident) table.Type {
|
||||||
return func.return_type
|
return func.return_type
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if ident.is_c {
|
||||||
|
return table.int_type
|
||||||
|
}
|
||||||
return table.void_type
|
return table.void_type
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
v.table
|
v.table
|
||||||
)
|
)
|
||||||
|
|
||||||
pub fn (p mut Parser) call_expr() ast.CallExpr {
|
pub fn (p mut Parser) call_expr(is_c bool) ast.CallExpr {
|
||||||
tok := p.tok
|
tok := p.tok
|
||||||
fn_name := p.check_name()
|
fn_name := p.check_name()
|
||||||
p.check(.lpar)
|
p.check(.lpar)
|
||||||
|
@ -18,6 +18,7 @@ pub fn (p mut Parser) call_expr() ast.CallExpr {
|
||||||
args: args
|
args: args
|
||||||
// tok: tok
|
// tok: tok
|
||||||
pos: tok.position()
|
pos: tok.position()
|
||||||
|
is_c: is_c
|
||||||
}
|
}
|
||||||
if p.tok.kind == .key_orelse {
|
if p.tok.kind == .key_orelse {
|
||||||
p.next()
|
p.next()
|
||||||
|
|
|
@ -545,7 +545,7 @@ pub fn (p mut Parser) name_expr() ast.Expr {
|
||||||
// fn call
|
// fn call
|
||||||
else {
|
else {
|
||||||
// println('calling $p.tok.lit')
|
// println('calling $p.tok.lit')
|
||||||
x := p.call_expr() // TODO `node,typ :=` should work
|
x := p.call_expr(is_c) // TODO `node,typ :=` should work
|
||||||
node = x
|
node = x
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -627,10 +627,11 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
|
||||||
p.next()
|
p.next()
|
||||||
}
|
}
|
||||||
.key_sizeof {
|
.key_sizeof {
|
||||||
p.next()
|
p.next() // sizeof
|
||||||
p.check(.lpar)
|
p.check(.lpar)
|
||||||
p.next()
|
type_name:= p.check_name()
|
||||||
p.check(.rpar)
|
p.check(.rpar)
|
||||||
|
node = ast.SizeOf{ type_name: type_name }
|
||||||
typ = table.int_type
|
typ = table.int_type
|
||||||
}
|
}
|
||||||
// Map `{"age": 20}` or `{ x | foo:bar, a:10 }`
|
// Map `{"age": 20}` or `{ x | foo:bar, a:10 }`
|
||||||
|
|
|
@ -8,14 +8,14 @@ module table
|
||||||
pub struct Table {
|
pub struct Table {
|
||||||
// struct_fields map[string][]string
|
// struct_fields map[string][]string
|
||||||
pub mut:
|
pub mut:
|
||||||
types []TypeSymbol
|
types []TypeSymbol
|
||||||
// type_idxs Hashmap
|
// type_idxs Hashmap
|
||||||
type_idxs map[string]int
|
type_idxs map[string]int
|
||||||
// fns Hashmap
|
// fns Hashmap
|
||||||
fns map[string]Fn
|
fns map[string]Fn
|
||||||
consts map[string]Var
|
consts map[string]Var
|
||||||
imports []string // List of all imports
|
imports []string // List of all imports
|
||||||
modules []string // List of all modules registered by the application
|
modules []string // List of all modules registered by the application
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Fn {
|
pub struct Fn {
|
||||||
|
@ -60,6 +60,7 @@ pub fn (t mut Table) register_global(name string, typ Type) {
|
||||||
// mod: p.mod
|
// mod: p.mod
|
||||||
// is_mut: true
|
// is_mut: true
|
||||||
// idx: -1
|
// idx: -1
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -362,7 +363,7 @@ pub fn (t &Table) check(got, expected Type) bool {
|
||||||
exp_type_sym := t.get_type_symbol(expected)
|
exp_type_sym := t.get_type_symbol(expected)
|
||||||
got_idx := type_idx(got)
|
got_idx := type_idx(got)
|
||||||
exp_idx := type_idx(expected)
|
exp_idx := type_idx(expected)
|
||||||
//println('check: $got_type_sym.name, $exp_type_sym.name')
|
// println('check: $got_type_sym.name, $exp_type_sym.name')
|
||||||
if exp_type_sym.kind == .voidptr {
|
if exp_type_sym.kind == .voidptr {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -372,6 +373,10 @@ pub fn (t &Table) check(got, expected Type) bool {
|
||||||
if got_type_sym.is_int() && exp_type_sym.is_int() {
|
if got_type_sym.is_int() && exp_type_sym.is_int() {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
// TODO
|
||||||
|
if got_type_sym.is_number() && exp_type_sym.is_number() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
if got_type_sym.kind == .array_fixed && exp_type_sym.kind == .byteptr {
|
if got_type_sym.kind == .array_fixed && exp_type_sym.kind == .byteptr {
|
||||||
info := got_type_sym.info as ArrayFixed
|
info := got_type_sym.info as ArrayFixed
|
||||||
if type_idx(info.elem_type) == byte_type_idx {
|
if type_idx(info.elem_type) == byte_type_idx {
|
||||||
|
@ -381,7 +386,8 @@ pub fn (t &Table) check(got, expected Type) bool {
|
||||||
// if expected.name == 'array' {
|
// if expected.name == 'array' {
|
||||||
// return true
|
// return true
|
||||||
// }
|
// }
|
||||||
if got_idx != exp_idx/*&& got.typ.name != expected.typ.name*/ {
|
if got_idx != exp_idx {
|
||||||
|
// && got.typ.name != expected.typ.name*/
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
|
Loading…
Reference in New Issue