v2: imports

pull/3721/head
Alexander Medvednikov 2020-02-12 01:16:38 +01:00
parent 0ec5680156
commit d9cf98f772
5 changed files with 53 additions and 6 deletions

View File

@ -460,7 +460,6 @@ pub fn (a []int) reduce(iter fn(accum, curr int)int, accum_start int) int {
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 {
@ -494,7 +493,6 @@ 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

View File

@ -311,6 +311,8 @@ pub fn (v mut V) compile() {
}
cgen.save()
v.cc()
//println(v.table.imports)
//println(v.table.modules)
}
pub fn (v mut V) compile2() {

View File

@ -90,6 +90,15 @@ pub fn (p mut Parser) parse_type() table.Type {
if p.tok.kind == .question {
p.next()
}
if p.peek_tok.kind == .dot {
///if !(p.tok.lit in p.table.imports) {
if !p.table.known_import(p.tok.lit) {
println(p.table.imports)
p.error('unknown module `$p.tok.lit`')
}
p.next()
p.check(.dot)
}
name := p.tok.lit
match p.tok.kind {
// func

View File

@ -211,6 +211,12 @@ pub fn (p mut Parser) top_stmt() ast.Stmt {
.hash {
return p.hash()
}
.key_type {
return p.type_decl()
}
.key_enum {
return p.enum_decl()
}
else {
p.error('parser: bad top level statement')
return ast.Stmt{}
@ -470,7 +476,7 @@ pub fn (p mut Parser) name_expr() (ast.Expr,table.Type) {
p.next()
p.check(.dot)
}
else if p.tok.lit in ['strings', 'strconv'] {
if p.peek_tok.kind == .dot && p.tok.lit in p.table.imports {
p.next()
p.check(.dot)
}
@ -1020,6 +1026,18 @@ fn (p mut Parser) string_expr() (ast.Expr,table.Type) {
fn (p mut Parser) array_init() (ast.Expr,table.Type) {
mut node := ast.Expr{}
p.check(.lsbr)
// `[]` - empty array with an automatically deduced type
/*
if p.peek_tok.kind == .rsbr && p.expected_type.kind == .array {
node = ast.ArrayInit{
// typ: array_type
exprs: []
pos: p.tok.position()
}
return node,p.expected_type
}
*/
mut val_type := table.void_type
mut exprs := []ast.Expr
mut is_fixed := false
@ -1103,16 +1121,17 @@ fn (p mut Parser) module_decl() ast.Module {
}
fn (p mut Parser) parse_import() ast.Import {
mod_name := p.check_name()
mut mod_name := p.check_name()
if p.tok.kind == .dot {
p.next()
p.check_name()
mod_name += '.' + p.check_name()
}
mut mod_alias := mod_name
if p.tok.kind == .key_as {
p.check(.key_as)
mod_alias = p.check_name()
}
p.table.imports << mod_name
return ast.Import{
mod: mod_name
alias: mod_alias
@ -1372,6 +1391,10 @@ fn (p mut Parser) global_decl() ast.GlobalDecl {
fn (p mut Parser) match_expr() (ast.Expr,table.Type) {
p.check(.key_match)
is_mut := p.tok.kind == .key_mut
if is_mut {
p.next()
}
cond,typ := p.expr(0)
p.check(.lcbr)
mut blocks := []ast.StmtBlock
@ -1443,6 +1466,10 @@ fn (p mut Parser) enum_decl() ast.EnumDecl {
val := p.check_name()
vals << val
p.warn('enum val $val')
if p.tok.kind == .assign {
p.next()
p.expr(0)
}
}
p.check(.rcbr)
return ast.EnumDecl{

View File

@ -19,7 +19,8 @@ pub mut:
fns map[string]Fn
consts map[string]Var
tmp_cnt int
imports []string
imports []string // List of all imports
modules []string // List of all modules registered by the application
}
pub struct Fn {
@ -577,3 +578,13 @@ pub fn (t &Table) get_expr_typ(expr ast.Expr) TypeSymbol {
}
}
*/
pub fn (t &Table) known_import(name string) bool {
for i in t.imports {
if i.all_after('.') == name {
return true
}
}
return false
}