mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
v2: the new parser can now parse the entire V compiler
This commit is contained in:
parent
4de8d3ae8c
commit
4e9bfa95ec
@ -7,6 +7,7 @@ import (
|
||||
benchmark
|
||||
os
|
||||
os.cmdline
|
||||
compiler
|
||||
)
|
||||
|
||||
fn compile(command string, args []string) {
|
||||
|
@ -396,5 +396,10 @@ fn (cflags []CFlag) msvc_string_flags() MsvcStringFlags {
|
||||
lpaths << '/LIBPATH:"' + os.realpath(l) + '"'
|
||||
}
|
||||
return MsvcStringFlags{
|
||||
real_libs,inc_paths,lpaths,other_flags}
|
||||
real_libs:real_libs
|
||||
inc_paths:inc_paths
|
||||
lib_paths:lpaths
|
||||
other_flags:other_flags
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -90,7 +90,9 @@ struct ScanRes {
|
||||
|
||||
fn scan_res(tok TokenKind, lit string) ScanRes {
|
||||
return ScanRes{
|
||||
tok,lit}
|
||||
tok:tok
|
||||
lit:lit
|
||||
}
|
||||
}
|
||||
|
||||
fn (s mut Scanner) ident_name() string {
|
||||
@ -127,7 +129,8 @@ fn filter_num_sep(txt byteptr, start int, end int) string {
|
||||
i++
|
||||
}
|
||||
b[i1]=0 // C string compatibility
|
||||
return string{b,i1}
|
||||
return string{str:b
|
||||
len:i1}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral |
|
||||
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
|
||||
AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr |
|
||||
CastExpr | EnumVal
|
||||
CastExpr | EnumVal | Assoc
|
||||
|
||||
pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
|
||||
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
|
||||
@ -447,6 +447,11 @@ pub:
|
||||
expr Expr
|
||||
}
|
||||
|
||||
pub struct Assoc {
|
||||
pub:
|
||||
name string
|
||||
}
|
||||
|
||||
// string representaiton of expr
|
||||
pub fn (x Expr) str() string {
|
||||
match x {
|
||||
|
@ -55,6 +55,7 @@ fn foo(a int) {
|
||||
number := nums[0]
|
||||
bools := [true, false]
|
||||
users := [User{}]
|
||||
//ptr := &User{}
|
||||
b := bools[0]
|
||||
mystrings := ['a', 'b']
|
||||
s := mystrings[0]
|
||||
|
@ -55,7 +55,7 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
|
||||
pref: &pref.Preferences{}
|
||||
scope: scope
|
||||
// scope: &ast.Scope{start_pos: 0, parent: 0}
|
||||
|
||||
|
||||
}
|
||||
p.init_parse_fns()
|
||||
p.read_first_token()
|
||||
@ -159,7 +159,7 @@ pub fn (p mut Parser) parse_block() []ast.Stmt {
|
||||
}
|
||||
}
|
||||
p.check(.rcbr)
|
||||
//println('parse block')
|
||||
// println('parse block')
|
||||
p.close_scope()
|
||||
// println('nr exprs in block = $exprs.len')
|
||||
return stmts
|
||||
@ -306,7 +306,7 @@ pub fn (p mut Parser) stmt() ast.Stmt {
|
||||
return ast.ExprStmt{
|
||||
expr: expr
|
||||
// typ: typ
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -327,6 +327,9 @@ pub fn (p mut Parser) assign_expr(left ast.Expr) ast.AssignExpr {
|
||||
|
||||
fn (p mut Parser) attr() ast.Attr {
|
||||
p.check(.lsbr)
|
||||
if p.tok.kind == .key_if {
|
||||
p.next()
|
||||
}
|
||||
name := p.check_name()
|
||||
p.check(.rsbr)
|
||||
return ast.Attr{
|
||||
@ -515,7 +518,7 @@ pub fn (p mut Parser) name_expr() ast.Expr {
|
||||
map_type := p.parse_map_type(0)
|
||||
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
|
||||
if p.peek_tok.kind == .lpar {
|
||||
name := p.tok.lit
|
||||
@ -546,7 +549,13 @@ pub fn (p mut Parser) name_expr() ast.Expr {
|
||||
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)) {
|
||||
return p.struct_init()
|
||||
}
|
||||
@ -600,7 +609,7 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
|
||||
node = p.match_expr()
|
||||
}
|
||||
.number {
|
||||
node, typ = p.parse_number_literal()
|
||||
node,typ = p.parse_number_literal()
|
||||
}
|
||||
.lpar {
|
||||
p.check(.lpar)
|
||||
@ -638,7 +647,7 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
|
||||
}
|
||||
}
|
||||
else {
|
||||
p.check_name()
|
||||
name := p.check_name()
|
||||
p.check(.pipe)
|
||||
for {
|
||||
p.check_name()
|
||||
@ -651,6 +660,9 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
|
||||
break
|
||||
}
|
||||
}
|
||||
node = ast.Assoc{
|
||||
name: name
|
||||
}
|
||||
}
|
||||
p.check(.rcbr)
|
||||
}
|
||||
@ -1033,10 +1045,10 @@ fn (p mut Parser) if_expr() ast.Expr {
|
||||
stmts: stmts
|
||||
else_stmts: else_stmts
|
||||
// typ: typ
|
||||
|
||||
|
||||
pos: p.tok.position()
|
||||
// left: left
|
||||
|
||||
|
||||
}
|
||||
return node
|
||||
}
|
||||
@ -1422,10 +1434,10 @@ fn (p mut Parser) var_decl() ast.VarDecl {
|
||||
node := ast.VarDecl{
|
||||
name: name
|
||||
expr: expr // p.expr(token.lowest_prec)
|
||||
|
||||
|
||||
is_mut: is_mut
|
||||
// typ: typ
|
||||
|
||||
|
||||
pos: p.tok.position()
|
||||
}
|
||||
p.scope.register_var(node)
|
||||
@ -1544,7 +1556,7 @@ fn (p mut Parser) match_expr() ast.Expr {
|
||||
blocks: blocks
|
||||
match_exprs: match_exprs
|
||||
// typ: typ
|
||||
|
||||
|
||||
cond: cond
|
||||
}
|
||||
return node
|
||||
|
Loading…
Reference in New Issue
Block a user