v/vlib/v/parser/parser.v

608 lines
11 KiB
V
Raw Normal View History

2019-12-22 02:34:37 +01:00
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module parser
import (
v.scanner
v.ast
v.token
v.table
2019-12-27 08:52:20 +01:00
v.types
2019-12-29 06:50:08 +01:00
term
2019-12-30 12:10:46 +01:00
os
2019-12-22 02:34:37 +01:00
)
struct Parser {
2019-12-28 09:43:22 +01:00
scanner &scanner.Scanner
2019-12-22 02:34:37 +01:00
mut:
2019-12-28 09:43:22 +01:00
tok token.Token
peek_tok token.Token
// vars []string
table &table.Table
2019-12-27 13:57:49 +01:00
return_type types.Type
2019-12-22 02:34:37 +01:00
}
2019-12-28 14:11:05 +01:00
pub fn parse_stmt(text string, table &table.Table) ast.Stmt {
s := scanner.new_scanner(text)
2019-12-22 02:34:37 +01:00
mut p := Parser{
scanner: s
table: table
2019-12-22 02:34:37 +01:00
}
p.read_first_token()
2019-12-28 14:11:05 +01:00
return p.stmt()
2019-12-22 02:34:37 +01:00
}
2019-12-27 13:57:49 +01:00
pub fn (p mut Parser) get_type() types.Type {
defer {
2019-12-28 09:43:22 +01:00
p.next()
2019-12-27 13:57:49 +01:00
}
match p.tok.lit {
2019-12-28 09:43:22 +01:00
'int' {
return types.int_type
}
'f64' {
return types.f64_type
}
'string' {
return types.string_type
}
else {
typ := p.table.types[p.tok.lit]
if isnil(typ.name.str) || typ.name == '' {
p.error('undefined type `$p.tok.lit`')
}
println('RET Typ $typ.name')
return typ
}
2019-12-27 13:57:49 +01:00
}
}
2019-12-30 12:10:46 +01:00
pub fn parse_file(text string, table &table.Table) ast.File {
2019-12-28 14:11:05 +01:00
mut stmts := []ast.Stmt
mut p := Parser{
2019-12-28 14:11:05 +01:00
scanner: scanner.new_scanner(text)
table: table
}
2019-12-28 14:11:05 +01:00
p.read_first_token()
for {
2019-12-28 09:43:22 +01:00
// res := s.scan()
if p.tok.kind == .eof {
break
}
2019-12-28 09:43:22 +01:00
// println('expr at ' + p.tok.str())
2019-12-28 14:11:05 +01:00
s := p.stmt()
// println(s)
2019-12-28 14:11:05 +01:00
stmts << s // p.stmt()
}
// println('nr stmts = $stmts.len')
2019-12-28 14:11:05 +01:00
// println(stmts[0])
2019-12-30 12:10:46 +01:00
return ast.File{
2019-12-28 14:11:05 +01:00
stmts: stmts
}
}
2019-12-30 12:10:46 +01:00
pub fn parse_files(paths []string, table &table.Table) []ast.File {
mut files := []ast.File
for path in paths {
mut stmts := []ast.Stmt
text := os.read_file(path) or { panic(err) }
mut p := Parser{
scanner: scanner.new_scanner(text)
table: table
}
p.read_first_token()
for {
// res := s.scan()
if p.tok.kind == .eof {
break
}
// println('expr at ' + p.tok.str())
s := p.stmt()
// println(s)
stmts << s // p.stmt()
}
// println('nr stmts = $stmts.len')
// println(stmts[0])
files << ast.File{
stmts: stmts
}
}
return files
}
2019-12-28 14:11:05 +01:00
pub fn (p mut Parser) read_first_token() {
// need to call next() twice to get peek token and current token
p.next()
p.next()
}
2019-12-28 14:11:05 +01:00
pub fn (p mut Parser) parse_block() []ast.Stmt {
mut stmts := []ast.Stmt
2019-12-27 13:57:49 +01:00
for {
2019-12-28 09:43:22 +01:00
// res := s.scan()
if p.tok.kind in [.eof, .rcbr] {
2019-12-27 13:57:49 +01:00
break
}
2019-12-28 09:43:22 +01:00
// println('expr at ' + p.tok.str())
2019-12-28 14:11:05 +01:00
stmts << p.stmt()
2019-12-27 13:57:49 +01:00
}
p.next()
2019-12-28 11:02:06 +01:00
// println('nr exprs in block = $exprs.len')
2019-12-28 14:11:05 +01:00
return stmts
2019-12-27 13:57:49 +01:00
}
2019-12-22 02:34:37 +01:00
fn (p mut Parser) next() {
p.tok = p.peek_tok
p.peek_tok = p.scanner.scan()
2019-12-24 18:54:43 +01:00
// println(p.tok.str())
2019-12-22 02:34:37 +01:00
}
fn (p mut Parser) check(expected token.TokenKind) {
if p.tok.kind != expected {
s := 'syntax error: unexpected `${p.tok.kind.str()}`, expecting `${expected.str()}`'
2019-12-29 07:24:17 +01:00
p.error(s)
2019-12-27 13:57:49 +01:00
}
p.next()
}
fn (p mut Parser) check_name() string {
name := p.tok.lit
2019-12-27 13:57:49 +01:00
p.check(.name)
return name
}
2019-12-28 14:11:05 +01:00
pub fn (p mut Parser) stmt() ast.Stmt {
2019-12-28 19:16:04 +01:00
// println('stmt at ' + p.tok.str())
2019-12-28 14:11:05 +01:00
// `x := ...`
2019-12-28 19:16:04 +01:00
if p.tok.kind == .name {
if p.peek_tok.kind == .decl_assign {
return p.var_decl()
}
else if p.peek_tok.is_assign() {
return p.assign_stmt()
}
2019-12-28 14:11:05 +01:00
}
match p.tok.kind {
2019-12-28 09:43:22 +01:00
.key_module {
return p.module_decl()
}
.key_import {
return p.import_stmt()
}
.key_fn {
return p.fn_decl()
}
.key_struct {
return p.struct_decl()
}
2019-12-28 09:43:22 +01:00
.key_return {
return p.return_stmt()
}
2019-12-28 11:02:06 +01:00
.key_mut {
return p.var_decl()
}
.key_for {
return p.for_statement()
}
2019-12-28 14:11:05 +01:00
else {
expr,_ := p.expr(0)
return ast.ExprStmt{
expr: expr
}
}
}
}
2019-12-28 19:16:04 +01:00
pub fn (p mut Parser) assign_stmt() ast.AssignStmt {
2019-12-29 06:50:08 +01:00
name := p.tok.lit
2019-12-29 07:24:17 +01:00
// println('looking for $name')
2019-12-29 06:50:08 +01:00
var := p.table.find_var(name) or {
p.error('unknown variable `$name`')
exit(1)
}
if !var.is_mut {
p.error('`$var.name` is immutable, declare it with `mut $var.name := ...`')
}
2019-12-28 19:16:04 +01:00
left_expr,left_type := p.expr(0)
op := p.tok.kind
2019-12-29 07:24:17 +01:00
// println('assignn_stmt() ' + op.str())
2019-12-28 19:16:04 +01:00
p.next()
right_expr,right_type := p.expr(0)
if !types.check(left_type, right_type) {
p.error('oops')
}
2019-12-28 19:16:04 +01:00
return ast.AssignStmt{
left: left_expr
right: right_expr
op: op
}
}
2019-12-29 06:50:08 +01:00
pub fn (p &Parser) error(s string) {
println(term.bold(term.red('x.v:$p.tok.line_nr: $s')))
exit(1)
}
2019-12-30 09:38:12 +01:00
pub fn (p &Parser) warn(s string) {
println(term.blue('x.v:$p.tok.line_nr: $s'))
}
pub fn (p mut Parser) call_expr() (ast.CallExpr,types.Type) {
// println('got fn call')
fn_name := p.tok.lit
f := p.table.find_fn(fn_name) or {
p.error('unknown function `$p.tok.lit`')
exit(0)
}
p.check(.name)
p.check(.lpar)
mut args := []ast.Expr
for i, arg in f.args {
e,typ := p.expr(0)
if !types.check(arg.typ, typ) {
p.error('cannot used type `$typ.name` as type `$arg.typ.name` in argument to `$fn_name`')
}
args << e
if i < f.args.len - 1 {
p.check(.comma)
}
}
if p.tok.kind == .comma {
p.error('too many arguments in call to `$fn_name`')
}
p.check(.rpar)
node := ast.CallExpr{
name: fn_name
args: args
}
return node,types.int_type
}
2019-12-28 14:11:05 +01:00
// Implementation of Pratt Precedence
pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) {
2019-12-28 19:16:04 +01:00
// println('expr at ' + p.tok.str())
2019-12-28 14:11:05 +01:00
// null denotation (prefix)
mut node := ast.Expr{}
mut typ := types.void_type
match p.tok.kind {
.name {
2019-12-29 07:24:17 +01:00
/*
sym := p.table.find_symbol(p.tok.lit)
if sym.cat == .function {
return
}
*/
// fn call
if p.peek_tok.kind == .lpar {
x,typ2 := p.call_expr() // TODO `node,typ :=` should work
node = x
typ = typ2
2019-12-29 07:24:17 +01:00
}
// struct init
else if p.peek_tok.kind == .lcbr {
typ = p.get_type()
p.check(.lcbr)
mut field_names := []string
mut exprs := []ast.Expr
for p.tok.kind != .rcbr {
field_name := p.check_name()
field_names << field_name
p.check(.colon)
expr,field_type := p.expr(0)
exprs << expr
}
node = ast.StructInit{
typ: typ
exprs: exprs
fields: field_names
}
p.check(.rcbr)
}
2019-12-29 07:24:17 +01:00
else {
// name expr
node = ast.Ident{
name: p.tok.lit
}
typ = types.int_type
p.next()
}
}
2019-12-30 09:38:12 +01:00
.lsbr {
node,typ = p.array_init()
}
.key_true, .key_false {
node = ast.BoolLiteral{
val: p.tok.kind == .key_true
}
typ = types.bool_type
p.next()
}
2019-12-28 09:43:22 +01:00
.str {
node,typ = p.parse_string_literal()
}
.number {
node,typ = p.parse_number_literal()
}
.key_if {
node,typ = p.if_expr()
}
.lpar {
node,typ = p.expr(0)
2019-12-28 11:02:06 +01:00
p.check(.rpar)
2019-12-27 13:57:49 +01:00
}
else {
if p.tok.is_unary() {
expr,_ := p.expr(token.highest_prec)
2019-12-24 18:54:43 +01:00
node = ast.UnaryExpr{
left: expr
2019-12-24 18:54:43 +01:00
}
}
2019-12-28 11:02:06 +01:00
else {
verror('!unknown token ' + p.tok.str())
}
}
}
// left binding power
for rbp < p.tok.precedence() {
prev_tok := p.tok
2019-12-22 02:34:37 +01:00
p.next()
2019-12-27 08:52:20 +01:00
mut t2 := types.Type{}
// left denotation (infix / postfix)
if prev_tok.is_right_assoc() {
mut expr := ast.Expr{}
expr,t2 = p.expr(prev_tok.precedence() - 1)
2019-12-24 18:54:43 +01:00
node = ast.BinaryExpr{
left: node
op: prev_tok.kind
right: expr
}
// println(t2.name + 'OOO')
2019-12-27 08:52:20 +01:00
if !types.check(&typ, &t2) {
println('tok: $prev_tok.str()')
2019-12-29 07:24:17 +01:00
p.error('cannot convert `$t2.name` to `$typ.name`')
2019-12-24 18:54:43 +01:00
}
}
else if prev_tok.is_left_assoc() {
// postfix (`++` | `--`)
if prev_tok.kind in [.inc, .dec] {
node = ast.UnaryExpr{
left: node
op: prev_tok.kind
}
} else {
mut expr := ast.Expr{}
expr,t2 = p.expr(prev_tok.precedence())
op := prev_tok.kind
if prev_tok.is_relational() {
typ = types.bool_type
}
else {
typ = t2
}
// println(t2.name + '222')
node = ast.BinaryExpr{
left: node
op: prev_tok.kind
right: expr
}
2019-12-24 18:54:43 +01:00
}
}
2019-12-22 02:34:37 +01:00
}
2019-12-28 09:43:22 +01:00
return node,typ
2019-12-24 18:54:43 +01:00
}
fn (p mut Parser) for_statement() ast.ForStmt {
p.check(.key_for)
cond,typ := p.expr(0)
if !types.check(types.bool_type, typ) {
p.error('non-bool used as for condition')
}
p.check(.lcbr)
stmts := p.parse_block()
return ast.ForStmt{
cond: cond
stmts: stmts
}
}
fn (p mut Parser) if_expr() (ast.Expr,types.Type) {
mut node := ast.Expr{}
p.check(.key_if)
cond,typ := p.expr(0)
if !types.check(types.bool_type, typ) {
p.error('non-bool used as if condition')
}
p.check(.lcbr)
stmts := p.parse_block()
node = ast.IfExpr{
cond: cond
stmts: stmts
}
return node,types.void_type
}
fn (p mut Parser) parse_string_literal() (ast.Expr,types.Type) {
mut node := ast.Expr{}
node = ast.StringLiteral{
val: p.tok.lit
}
p.next()
2019-12-28 09:43:22 +01:00
return node,types.string_type
}
2019-12-30 09:38:12 +01:00
fn (p mut Parser) array_init() (ast.Expr,types.Type) {
p.check(.lsbr)
mut val_type := types.void_type
mut exprs := []ast.Expr
mut i := 0
for p.tok.kind != .rsbr {
expr,typ := p.expr(0)
// The first element's type
if i == 0 {
val_type = typ
}
else if !types.check(val_type, typ) {
p.error('expected array element with type `$val_type.name`')
}
exprs << expr
i++
if p.tok.kind == .comma {
p.check(.comma)
}
}
mut node := ast.Expr{}
node = ast.ArrayInit{
typ: val_type
exprs: exprs
}
p.check(.rsbr)
return node,val_type
}
fn (p mut Parser) parse_number_literal() (ast.Expr,types.Type) {
lit := p.tok.lit
mut node := ast.Expr{}
mut typ := types.int_type
if lit.contains('.') {
node = ast.FloatLiteral{
2019-12-28 09:43:22 +01:00
// val: lit.f64()
val: lit
}
2019-12-28 11:02:06 +01:00
typ = types.f64_type
2019-12-28 09:43:22 +01:00
}
else {
node = ast.IntegerLiteral{
val: lit.int()
}
typ = types.int_type
}
p.next()
2019-12-28 09:43:22 +01:00
return node,typ
}
2019-12-30 12:10:46 +01:00
fn (p mut Parser) module_decl() ast.Module {
p.check(.key_module)
p.next()
2019-12-28 14:11:05 +01:00
return ast.Module{}
2019-12-28 09:43:22 +01:00
}
2019-12-28 14:11:05 +01:00
fn (p mut Parser) import_stmt() ast.Import {
// p.check(.key_import)
p.next()
2019-12-28 14:11:05 +01:00
return ast.Import{}
}
fn (p mut Parser) struct_decl() ast.StructDecl {
p.check(.key_struct)
name := p.check_name()
p.check(.lcbr)
mut fields := []ast.Field
for p.tok.kind != .rcbr {
field_name := p.check_name()
typ := p.get_type()
fields << ast.Field{
name: field_name
typ: typ
}
}
p.check(.rcbr)
p.table.register_type(types.Type{
name: name
})
return ast.StructDecl{
name: name
fields: fields
}
}
2019-12-28 14:11:05 +01:00
fn (p mut Parser) fn_decl() ast.FnDecl {
2019-12-29 06:50:08 +01:00
p.table.clear_vars()
p.check(.key_fn)
name := p.check_name()
2019-12-28 11:02:06 +01:00
// println('fn decl $name')
p.check(.lpar)
2019-12-29 07:24:17 +01:00
// Args
mut args := []table.Var
mut ast_args := []ast.Arg
for p.tok.kind != .rpar {
arg_name := p.check_name()
typ := p.get_type()
args << table.Var{
name: arg_name
typ: typ
}
ast_args << ast.Arg{
typ: typ
name: arg_name
}
}
p.check(.rpar)
// Return type
mut typ := types.void_type
if p.tok.kind == .name {
typ = p.get_type()
p.return_type = typ
}
p.check(.lcbr)
2019-12-29 07:24:17 +01:00
p.table.register_fn(table.Fn{
name: name
args: args
})
2019-12-28 14:11:05 +01:00
stmts := p.parse_block()
return ast.FnDecl{
2019-12-28 09:43:22 +01:00
name: name
2019-12-28 14:11:05 +01:00
stmts: stmts
2019-12-28 09:43:22 +01:00
typ: typ
2019-12-29 07:24:17 +01:00
args: ast_args
2019-12-28 09:43:22 +01:00
}
}
2019-12-28 14:11:05 +01:00
fn (p mut Parser) return_stmt() ast.Return {
p.next()
2019-12-28 09:43:22 +01:00
expr,t := p.expr(0)
if !types.check(p.return_type, t) {
2019-12-29 07:24:17 +01:00
p.error('bad ret type')
}
2019-12-28 14:11:05 +01:00
return ast.Return{
2019-12-28 09:43:22 +01:00
expr: expr
}
}
2019-12-28 14:11:05 +01:00
fn (p mut Parser) var_decl() ast.VarDecl {
2019-12-28 11:02:06 +01:00
is_mut := p.tok.kind == .key_mut // || p.prev_tok == .key_for
// is_static := p.tok.kind == .key_static
2019-12-28 11:02:06 +01:00
if p.tok.kind == .key_mut {
p.check(.key_mut)
// p.fspace()
}
if p.tok.kind == .key_static {
p.check(.key_static)
// p.fspace()
}
name := p.tok.lit
2019-12-28 14:11:05 +01:00
p.read_first_token()
2019-12-28 09:43:22 +01:00
expr,t := p.expr(token.lowest_prec)
2019-12-29 06:50:08 +01:00
if _ := p.table.find_var(name) {
p.error('redefinition of `$name`')
}
2019-12-29 06:50:08 +01:00
p.table.register_var(table.Var{
name: name
is_mut: is_mut
})
2019-12-28 11:02:06 +01:00
// println(p.table.names)
// println('added $name')
2019-12-28 14:11:05 +01:00
return ast.VarDecl{
name: name
2019-12-28 09:43:22 +01:00
expr: expr // p.expr(token.lowest_prec)
2019-12-30 12:10:46 +01:00
typ: t
2019-12-28 14:11:05 +01:00
}
}
2019-12-26 11:21:41 +01:00
fn verror(s string) {
println(s)
exit(1)
}