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

v2: parse builtin.v, cfns.v float.v

This commit is contained in:
Alexander Medvednikov
2020-02-04 09:54:15 +01:00
parent 432ee93916
commit 83f0c228e9
11 changed files with 122 additions and 44 deletions

View File

@@ -50,13 +50,19 @@ pub fn (p mut Parser) call_args() []ast.Expr {
return args // ,table.void_ti
}
fn (p mut Parser) fn_decl(/*high bool*/) ast.FnDecl {
fn (p mut Parser) fn_decl() ast.FnDecl {
is_pub := p.tok.kind == .key_pub
if is_pub {
p.next()
}
p.table.clear_vars()
p.check(.key_fn)
// C.
is_c := p.tok.kind == .name && p.tok.lit == 'C'
if is_c {
p.next()
p.check(.dot)
}
// Receiver?
mut rec_name := ''
mut is_method := false
@@ -91,37 +97,50 @@ fn (p mut Parser) fn_decl(/*high bool*/) ast.FnDecl {
// Args
mut args := []table.Var
mut ast_args := []ast.Arg
for p.tok.kind != .rpar {
mut arg_names := [p.check_name()]
// `a, b, c int`
for p.tok.kind == .comma {
p.check(.comma)
arg_names << p.check_name()
}
ti := p.parse_type()
for arg_name in arg_names {
arg := table.Var{
name: arg_name
typ: ti
}
args << arg
p.table.register_var(arg)
ast_args << ast.Arg{
ti: ti
name: arg_name
}
if ti.kind == .variadic && p.tok.kind == .comma {
p.error('cannot use ...(variadic) with non-final parameter $arg_name')
// `int, int, string` (no names, just types)
types_only := p.tok.kind == .amp || (p.peek_tok.kind == .comma && p.table.known_type(p.tok.lit)) || p.peek_tok.kind == .rpar
if types_only {
p.warn('types only')
for p.tok.kind != .rpar {
p.parse_type()
if p.tok.kind == .comma {
p.next()
}
}
if p.tok.kind != .rpar {
p.check(.comma)
}
else {
for p.tok.kind != .rpar {
mut arg_names := [p.check_name()]
// `a, b, c int`
for p.tok.kind == .comma {
p.check(.comma)
arg_names << p.check_name()
}
ti := p.parse_type()
for arg_name in arg_names {
arg := table.Var{
name: arg_name
typ: ti
}
args << arg
p.table.register_var(arg)
ast_args << ast.Arg{
ti: ti
name: arg_name
}
if ti.kind == .variadic && p.tok.kind == .comma {
p.error('cannot use ...(variadic) with non-final parameter $arg_name')
}
}
if p.tok.kind != .rpar {
p.check(.comma)
}
}
}
p.check(.rpar)
// Return type
mut typ := table.void_type
if p.tok.kind in [.name, .lpar] {
if p.tok.kind in [.name, .lpar, .amp] {
typ = p.parse_type()
p.return_type = typ
}
@@ -145,7 +164,10 @@ fn (p mut Parser) fn_decl(/*high bool*/) ast.FnDecl {
return_type: typ
})
}
stmts := p.parse_block()
mut stmts := []ast.Stmt
if p.tok.kind == .lcbr {
stmts = p.parse_block()
}
return ast.FnDecl{
name: name
stmts: stmts

View File

@@ -2,7 +2,6 @@ module parser
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
import (
v.table
)
@@ -66,7 +65,7 @@ pub fn (p mut Parser) parse_variadic_ti() table.Type {
}
pub fn (p mut Parser) parse_fn_type() table.Type {
//p.check(.key_fn)
// p.check(.key_fn)
p.fn_decl()
return table.int_type
}
@@ -77,6 +76,10 @@ pub fn (p mut Parser) parse_type() table.Type {
p.check(.amp)
nr_muls++
}
if p.tok.lit == 'C' {
p.next()
p.check(.dot)
}
name := p.tok.lit
match p.tok.kind {
// func
@@ -173,4 +176,3 @@ pub fn (p mut Parser) parse_type() table.Type {
}
}
}

View File

@@ -195,8 +195,11 @@ pub fn (p mut Parser) top_stmt() ast.Stmt {
.dollar {
return p.comp_if()
}
.hash {
return p.hash()
}
else {
p.error('bad top level statement')
p.error('parser: bad top level statement')
return ast.Stmt{}
}
}
@@ -457,8 +460,15 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
.str {
node,typ = p.string_expr()
}
// -1, -a etc
.minus, .amp, .mul {
.chartoken {
typ = table.byte_type
node = ast.CharLiteral{
val: p.tok.lit
}
p.next()
}
// -1, -a, !x, &x etc
.minus, .amp, .mul, .not {
node,typ = p.prefix_expr()
}
// .amp {
@@ -485,6 +495,13 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
.lsbr {
node,typ = p.array_init()
}
.key_sizeof {
p.next()
p.check(.lpar)
p.next()
p.check(.rpar)
typ = table.int_type
}
else {
p.error('pexpr(): bad token `$p.tok.str()`')
}
@@ -755,6 +772,9 @@ fn (p mut Parser) string_expr() (ast.Expr,table.Type) {
}
p.check(.str_dollar)
p.expr(0)
if p.tok.kind == .semicolon {
p.next()
}
}
return node,table.string_type
}
@@ -994,6 +1014,13 @@ fn (p mut Parser) var_decl() ast.VarDecl {
return node
}
fn (p mut Parser) hash() ast.HashStmt {
p.next()
return ast.HashStmt{
name: p.tok.lit
}
}
fn (p mut Parser) global_decl() ast.GlobalDecl {
if !p.pref.translated && !p.pref.is_live && !p.builtin_mod && !p.pref.building_v && p.mod != 'ui' && p.mod != 'gg2' && p.mod != 'uiold' && !os.getwd().contains('/volt') && !p.pref.enable_globals {
p.error('use `v --enable-globals ...` to enable globals')