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 |
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
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 struct StructType {
// fields []Field
@ -127,6 +127,10 @@ pub:
is_c bool
}
pub struct InterfaceDecl {
name string
}
pub struct StructInit {
pub:
pos token.Position

View File

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

View File

@ -275,6 +275,9 @@ pub fn (p mut Parser) top_stmt() ast.Stmt {
.lsbr {
return p.attribute()
}
.key_interface {
return p.interface_decl()
}
.key_module {
return p.module_decl()
}
@ -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 {
p.next()
// return expressions

View File

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

View File

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