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