v2: initial interface support; fix enum_hex_test.v

pull/4170/head
Alexander Medvednikov 2020-03-31 19:59:38 +02:00
parent 50143ad9bf
commit 2b563bc69f
5 changed files with 92 additions and 46 deletions

View File

@ -19,7 +19,7 @@ Type | AsCast | TypeOf | StringInterLiteral
pub type Stmt = GlobalDecl | FnDecl | Return | Module | Import | ExprStmt | pub type Stmt = GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt | ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt | HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt |
LineComment | MultiLineComment | AssertStmt | UnsafeStmt | GoStmt | Block LineComment | MultiLineComment | AssertStmt | UnsafeStmt | GoStmt | Block | InterfaceDecl
// pub type Type = StructType | ArrayType // pub type Type = StructType | ArrayType
// pub struct StructType { // pub struct StructType {
// fields []Field // fields []Field
@ -127,6 +127,10 @@ pub:
is_c bool is_c bool
} }
pub struct InterfaceDecl {
name string
}
pub struct StructInit { pub struct StructInit {
pub: pub:
pos token.Position pos token.Position

View File

@ -406,6 +406,9 @@ fn (g mut Gen) stmt(node ast.Stmt) {
} }
} }
ast.Import {} ast.Import {}
ast.InterfaceDecl {
g.writeln('// interface')
}
ast.Return { ast.Return {
if g.defer_stmts.len > 0 { if g.defer_stmts.len > 0 {
g.write_defer_stmts() g.write_defer_stmts()

View File

@ -52,7 +52,7 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
pref: &pref.Preferences{} pref: &pref.Preferences{}
scope: scope scope: scope
// scope: &ast.Scope{start_pos: 0, parent: 0} // scope: &ast.Scope{start_pos: 0, parent: 0}
} }
p.init_parse_fns() p.init_parse_fns()
p.read_first_token() p.read_first_token()
@ -71,13 +71,13 @@ pub fn parse_file(path string, table &table.Table, comments_mode scanner.Comment
table: table table: table
file_name: path file_name: path
pref: pref // &pref.Preferences{} pref: pref // &pref.Preferences{}
scope: &ast.Scope{ scope: &ast.Scope{
start_pos: 0 start_pos: 0
parent: 0 parent: 0
} }
// comments_mode: comments_mode // comments_mode: comments_mode
} }
p.read_first_token() p.read_first_token()
// p.scope = &ast.Scope{start_pos: p.tok.position(), parent: 0} // p.scope = &ast.Scope{start_pos: p.tok.position(), parent: 0}
@ -275,6 +275,9 @@ pub fn (p mut Parser) top_stmt() ast.Stmt {
.lsbr { .lsbr {
return p.attribute() return p.attribute()
} }
.key_interface {
return p.interface_decl()
}
.key_module { .key_module {
return p.module_decl() return p.module_decl()
} }
@ -688,7 +691,7 @@ pub fn (p mut Parser) name_expr() ast.Expr {
p.expr_mod = '' p.expr_mod = ''
return ast.EnumVal{ return ast.EnumVal{
enum_name: enum_name // lp.prepend_mod(enum_name) enum_name: enum_name // lp.prepend_mod(enum_name)
val: val val: val
pos: p.tok.position() pos: p.tok.position()
mod: mod mod: mod
@ -778,7 +781,7 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr {
node = ast.SizeOf{ node = ast.SizeOf{
typ: sizeof_type typ: sizeof_type
// type_name: type_name // type_name: type_name
} }
} }
.key_typeof { .key_typeof {
@ -1049,7 +1052,7 @@ fn (p mut Parser) infix_expr(left ast.Expr) ast.Expr {
left: left left: left
right: right right: right
// right_type: typ // right_type: typ
op: op op: op
pos: pos pos: pos
} }
@ -1454,7 +1457,7 @@ fn (p mut Parser) const_decl() ast.ConstDecl {
fields << ast.Field{ fields << ast.Field{
name: name name: name
// typ: typ // typ: typ
} }
exprs << expr exprs << expr
// TODO: once consts are fixed reg here & update in checker // TODO: once consts are fixed reg here & update in checker
@ -1581,6 +1584,28 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
} }
} }
fn (p mut Parser) interface_decl() ast.InterfaceDecl {
is_pub := p.tok.kind == .key_pub
if is_pub {
p.next()
}
p.next() // `interface`
interface_name := p.check_name()
p.check(.lcbr)
for p.tok.kind != .rcbr && p.tok.kind != .eof {
line_nr := p.tok.line_nr
name := p.check_name()
p.fn_args()
if p.tok.kind == .name && p.tok.line_nr == line_nr {
p.parse_type()
}
}
p.check(.rcbr)
return ast.InterfaceDecl{
name: interface_name
}
}
fn (p mut Parser) return_stmt() ast.Return { fn (p mut Parser) return_stmt() ast.Return {
p.next() p.next()
// return expressions // return expressions

View File

@ -1,10 +1,10 @@
enum w_hex { enum WHex {
a = 0x001 a = 0x001
b = 0x010 b = 0x010
c = 0x100 c = 0x100
} }
enum w_decimal { enum WDecimal {
a = 1 a = 1
b = 16 b = 16
c = 256 c = 256
@ -16,18 +16,14 @@ const (
cc = 256 cc = 256
) )
fn test_enum_hex() { fn test_enum_hex() {
assert ca == int(w_decimal.a) assert ca == int(WDecimal.a)
assert cb == int(w_decimal.b) assert cb == int(WDecimal.b)
assert cc == int(w_decimal.c) assert cc == int(WDecimal.c)
assert int(WHex.a) == ca
assert int(w_hex.a) == ca assert int(WHex.b) == cb
assert int(w_hex.b) == cb assert int(WHex.c) == cc
assert int(w_hex.c) == cc assert int(WHex.a) == int(WDecimal.a)
assert int(WHex.b) == int(WDecimal.b)
assert int(w_hex.a) == int(w_decimal.a) assert int(WHex.c) == int(WDecimal.c)
assert int(w_hex.b) == int(w_decimal.b)
assert int(w_hex.c) == int(w_decimal.c)
} }

View File

@ -3,32 +3,44 @@ struct Dog {
} }
struct Cat { struct Cat {
breed string breed string
} }
fn (d Cat) name() string {
return 'Cat'
}
fn (d Cat) name() string { return 'Cat' } fn (d Cat) speak() {
fn (d Cat) speak() { println('meow') } println('meow')
}
fn (d Dog) speak() { println('woof') } fn (d Dog) speak() {
fn (d Dog) name() string { return 'Dog'} println('woof')
}
fn (d Dog) name() string {
return 'Dog'
}
/*
interface Speaker { interface Speaker {
name() string name ()string
speak() speak()}
}
interface Speak2er { interface Speak2er {
name() string name ()string
speak() speak()}
}
struct Foo { struct Foo {
speaker Speaker speaker Speaker
speakers []Speaker speakers []Speaker
} }
fn perform_speak(s Speaker) { fn perform_speak(s Speaker) {
if true {
// QTODO
return
}
s.speak() s.speak()
assert true assert true
name := s.name() name := s.name()
@ -36,16 +48,18 @@ fn perform_speak(s Speaker) {
println(s.name()) println(s.name())
} }
fn perform_speakers(speakers []Speaker) { fn perform_speakers(speakers []Speaker) {}
}
fn test_perform_speak() { fn test_perform_speak() {
if true {
// QTODO
return
}
dog := Dog{} dog := Dog{}
perform_speak(dog) perform_speak(dog)
cat := Cat{} cat := Cat{}
perform_speak(cat) perform_speak(cat)
//perform_speakers([dog, cat]) // perform_speakers([dog, cat])
/* /*
f := Foo { f := Foo {
speaker: dog speaker: dog
@ -55,19 +69,23 @@ fn test_perform_speak() {
} }
interface Register { interface Register {
register() register()}
struct RegTest {
a int
} }
struct RegTest {a int} fn (f RegTest) register() {}
fn (f RegTest) register() { fn handle_reg(r Register) {}
}
fn handle_reg(r Register) {
}
fn test_register() { fn test_register() {
if true {
// QTODO
return
}
f := RegTest{} f := RegTest{}
f.register() f.register()
handle_reg(f) handle_reg(f)
} }
*/