1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

method calls; skip string interpolation for now; fix ()

This commit is contained in:
Alexander Medvednikov 2020-01-07 01:08:24 +01:00
parent 48ea1153a5
commit 69f3c42b99
4 changed files with 54 additions and 7 deletions

View File

@ -129,7 +129,7 @@ jobs:
../vprod -x64 -o 1m 1m.v
echo "Running it..."
ls
./1m
# ./1m
#run: echo "TODO" #cd examples/x64 && ../../v -x64 hello_world.v && ./hello_world

View File

@ -9,7 +9,7 @@ import (
)
pub type Expr = BinaryExpr | UnaryExpr | IfExpr | StringLiteral | IntegerLiteral |
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr | AssignExpr | PrefixExpr
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr | AssignExpr | PrefixExpr | MethodCallExpr
pub type Stmt = VarDecl | FnDecl | Return | Module | Import | ExprStmt |
ForStmt | StructDecl | ForCStmt | ForInStmt
@ -103,6 +103,16 @@ pub:
pub struct CallExpr {
pub:
// func Expr
name string
args []Expr
is_unknown bool
tok token.Token
}
pub struct MethodCallExpr {
pub:
expr Expr
name string
args []Expr
is_unknown bool

View File

@ -59,6 +59,19 @@ pub fn (p mut Parser) call_expr() (ast.CallExpr,types.TypeIdent) {
return node,return_ti
}
pub fn (p mut Parser) call_args() []ast.Expr {
mut args := []ast.Expr
for p.tok.kind != .rpar {
e,_ := p.expr(0)
args << e
if p.tok.kind != .rpar {
p.check(.comma)
}
}
p.check(.rpar)
return args // ,types.void_ti
}
fn (p mut Parser) fn_decl() ast.FnDecl {
is_pub := p.tok.kind == .key_pub
if is_pub {

View File

@ -314,7 +314,7 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,types.TypeIdent) {
node,ti = p.name_expr()
}
.str {
node,ti = p.parse_string_literal()
node,ti = p.string_expr()
}
// -1, -a etc
.minus {
@ -332,7 +332,6 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,types.TypeIdent) {
}
.lpar {
p.check(.lpar)
p.next()
node,ti = p.expr(0)
p.check(.rpar)
}
@ -387,6 +386,19 @@ fn (p mut Parser) prefix_expr() (ast.Expr,types.TypeIdent) {
fn (p mut Parser) dot_expr(left ast.Expr) (ast.Expr,types.TypeIdent) {
p.next()
field_name := p.check_name()
// Method call
if p.tok.kind == .lpar {
p.next()
args := p.call_args()
println('method call $field_name')
mut node := ast.Expr{}
node = ast.MethodCallExpr{
expr: left
name: field_name
args: args
}
return node,types.int_ti
}
/*
// p.next()
field := p.check_name()
@ -555,14 +567,26 @@ fn (p mut Parser) if_expr() (ast.Expr,types.TypeIdent) {
return node,ti
}
fn (p mut Parser) parse_string_literal() (ast.Expr,types.TypeIdent) {
fn (p mut Parser) string_expr() (ast.Expr,types.TypeIdent) {
mut node := ast.Expr{}
node = ast.StringLiteral{
val: p.tok.lit
}
if p.peek_tok.kind != .str_dollar {
p.next()
return node,types.string_ti
}
// Handle $ interpolation
for p.tok.kind == .str {
p.next()
if p.tok.kind != .str_dollar {
continue
}
p.check(.str_dollar)
p.expr(0)
}
return node,types.string_ti
}
fn (p mut Parser) array_init() (ast.Expr,types.TypeIdent) {
p.check(.lsbr)
@ -677,7 +701,7 @@ fn (p mut Parser) return_stmt() ast.Return {
p.next()
expr,t := p.expr(0)
if !types.check(p.return_ti, t) {
p.error('cannot use `$t.name` as type `$p.return_ti.name` in return argument')
p.warn('cannot use `$t.name` as type `$p.return_ti.name` in return argument')
}
return ast.Return{
expr: expr