v2: the new parser can now parse the entire V compiler

pull/3772/head
Alexander Medvednikov 2020-02-18 17:29:47 +01:00
parent 4de8d3ae8c
commit 4e9bfa95ec
6 changed files with 43 additions and 16 deletions

View File

@ -7,6 +7,7 @@ import (
benchmark benchmark
os os
os.cmdline os.cmdline
compiler
) )
fn compile(command string, args []string) { fn compile(command string, args []string) {

View File

@ -396,5 +396,10 @@ fn (cflags []CFlag) msvc_string_flags() MsvcStringFlags {
lpaths << '/LIBPATH:"' + os.realpath(l) + '"' lpaths << '/LIBPATH:"' + os.realpath(l) + '"'
} }
return MsvcStringFlags{ return MsvcStringFlags{
real_libs,inc_paths,lpaths,other_flags} real_libs:real_libs
inc_paths:inc_paths
lib_paths:lpaths
other_flags:other_flags
}
} }

View File

@ -90,7 +90,9 @@ struct ScanRes {
fn scan_res(tok TokenKind, lit string) ScanRes { fn scan_res(tok TokenKind, lit string) ScanRes {
return ScanRes{ return ScanRes{
tok,lit} tok:tok
lit:lit
}
} }
fn (s mut Scanner) ident_name() string { fn (s mut Scanner) ident_name() string {
@ -127,7 +129,8 @@ fn filter_num_sep(txt byteptr, start int, end int) string {
i++ i++
} }
b[i1]=0 // C string compatibility b[i1]=0 // C string compatibility
return string{b,i1} return string{str:b
len:i1}
} }
} }

View File

@ -11,7 +11,7 @@ import (
pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral | pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral |
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr | FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr | AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr |
CastExpr | EnumVal CastExpr | EnumVal | Assoc
pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt | pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt | ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
@ -447,6 +447,11 @@ pub:
expr Expr expr Expr
} }
pub struct Assoc {
pub:
name string
}
// string representaiton of expr // string representaiton of expr
pub fn (x Expr) str() string { pub fn (x Expr) str() string {
match x { match x {

View File

@ -55,6 +55,7 @@ fn foo(a int) {
number := nums[0] number := nums[0]
bools := [true, false] bools := [true, false]
users := [User{}] users := [User{}]
//ptr := &User{}
b := bools[0] b := bools[0]
mystrings := ['a', 'b'] mystrings := ['a', 'b']
s := mystrings[0] s := mystrings[0]

View File

@ -159,7 +159,7 @@ pub fn (p mut Parser) parse_block() []ast.Stmt {
} }
} }
p.check(.rcbr) p.check(.rcbr)
//println('parse block') // println('parse block')
p.close_scope() p.close_scope()
// println('nr exprs in block = $exprs.len') // println('nr exprs in block = $exprs.len')
return stmts return stmts
@ -327,6 +327,9 @@ pub fn (p mut Parser) assign_expr(left ast.Expr) ast.AssignExpr {
fn (p mut Parser) attr() ast.Attr { fn (p mut Parser) attr() ast.Attr {
p.check(.lsbr) p.check(.lsbr)
if p.tok.kind == .key_if {
p.next()
}
name := p.check_name() name := p.check_name()
p.check(.rsbr) p.check(.rsbr)
return ast.Attr{ return ast.Attr{
@ -515,7 +518,7 @@ pub fn (p mut Parser) name_expr() ast.Expr {
map_type := p.parse_map_type(0) map_type := p.parse_map_type(0)
return node return node
} }
// p.warn('name expr $p.tok.lit') // p.warn('name expr $p.tok.lit $p.peek_tok.str()')
// fn call or type cast // fn call or type cast
if p.peek_tok.kind == .lpar { if p.peek_tok.kind == .lpar {
name := p.tok.lit name := p.tok.lit
@ -546,7 +549,13 @@ pub fn (p mut Parser) name_expr() ast.Expr {
node = x node = x
} }
} }
else if p.peek_tok.kind == .lcbr && (p.tok.lit[0].is_capital() || is_c || p.tok.lit in ['array', 'string', 'ustring', 'mapnode', 'map']) && !p.tok.lit[p.tok.lit.len - 1].is_capital() { else if p.peek_tok.kind == .lcbr && (p.tok.lit[0].is_capital() || is_c ||
//
p.tok.lit in ['array', 'string', 'ustring', 'mapnode', 'map']) &&
//
(p.tok.lit.len == 1 || !p.tok.lit[p.tok.lit.len - 1].is_capital())
//
{
// || p.table.known_type(p.tok.lit)) { // || p.table.known_type(p.tok.lit)) {
return p.struct_init() return p.struct_init()
} }
@ -600,7 +609,7 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
node = p.match_expr() node = p.match_expr()
} }
.number { .number {
node, typ = p.parse_number_literal() node,typ = p.parse_number_literal()
} }
.lpar { .lpar {
p.check(.lpar) p.check(.lpar)
@ -638,7 +647,7 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
} }
} }
else { else {
p.check_name() name := p.check_name()
p.check(.pipe) p.check(.pipe)
for { for {
p.check_name() p.check_name()
@ -651,6 +660,9 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
break break
} }
} }
node = ast.Assoc{
name: name
}
} }
p.check(.rcbr) p.check(.rcbr)
} }