2020-01-23 21:04:46 +01:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-12-22 02:34:37 +01:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module parser
|
|
|
|
|
|
|
|
import (
|
2019-12-27 05:43:17 +01:00
|
|
|
v.scanner
|
|
|
|
v.ast
|
|
|
|
v.token
|
|
|
|
v.table
|
2020-02-03 07:02:54 +01:00
|
|
|
v.pref
|
2019-12-29 06:50:08 +01:00
|
|
|
term
|
2019-12-30 12:10:46 +01:00
|
|
|
os
|
2020-02-17 20:31:23 +01:00
|
|
|
filepath
|
2019-12-22 02:34:37 +01:00
|
|
|
)
|
2020-01-22 21:34:38 +01:00
|
|
|
|
2020-01-09 14:08:33 +01:00
|
|
|
const (
|
|
|
|
colored_output = term.can_show_color_on_stderr()
|
|
|
|
)
|
2020-02-05 10:00:11 +01:00
|
|
|
/*
|
2020-01-06 16:13:12 +01:00
|
|
|
type PrefixParseFn fn()ast.Expr
|
|
|
|
|
|
|
|
type InfixParseFn fn(e ast.Expr)ast.Expr
|
|
|
|
|
|
|
|
type PostfixParseFn fn()ast.Expr
|
2020-02-05 10:00:11 +01:00
|
|
|
*/
|
|
|
|
|
2020-01-06 16:13:12 +01:00
|
|
|
|
2019-12-22 02:34:37 +01:00
|
|
|
struct Parser {
|
2020-02-16 11:48:29 +01:00
|
|
|
scanner &scanner.Scanner
|
|
|
|
file_name string
|
2019-12-22 02:34:37 +01:00
|
|
|
mut:
|
2020-02-16 11:48:29 +01:00
|
|
|
tok token.Token
|
|
|
|
peek_tok token.Token
|
2019-12-28 09:43:22 +01:00
|
|
|
// vars []string
|
2020-02-16 11:48:29 +01:00
|
|
|
table &table.Table
|
|
|
|
return_type table.Type // current function's return type
|
|
|
|
is_c bool
|
2020-01-06 16:13:12 +01:00
|
|
|
//
|
2020-01-07 01:57:38 +01:00
|
|
|
// prefix_parse_fns []PrefixParseFn
|
2020-02-16 11:48:29 +01:00
|
|
|
inside_if bool
|
|
|
|
pref &pref.Preferences // Preferences shared from V struct
|
|
|
|
builtin_mod bool
|
|
|
|
mod string
|
2020-02-27 00:12:37 +01:00
|
|
|
expr_mod string
|
2020-02-16 11:48:29 +01:00
|
|
|
expected_type table.Type
|
|
|
|
scope &ast.Scope
|
2020-02-19 03:08:10 +01:00
|
|
|
imports map[string]string
|
2019-12-22 02:34:37 +01:00
|
|
|
}
|
|
|
|
|
2020-01-22 21:34:38 +01:00
|
|
|
// for tests
|
2020-02-15 13:37:48 +01:00
|
|
|
pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
|
2019-12-28 09:15:32 +01:00
|
|
|
s := scanner.new_scanner(text)
|
2019-12-22 02:34:37 +01:00
|
|
|
mut p := Parser{
|
|
|
|
scanner: s
|
2019-12-26 11:27:35 +01:00
|
|
|
table: table
|
2020-02-07 22:10:48 +01:00
|
|
|
pref: &pref.Preferences{}
|
2020-02-15 13:37:48 +01:00
|
|
|
scope: scope
|
2020-02-16 11:48:29 +01:00
|
|
|
// scope: &ast.Scope{start_pos: 0, parent: 0}
|
2020-02-28 15:36:41 +01:00
|
|
|
|
2019-12-22 02:34:37 +01:00
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
p.init_parse_fns()
|
2019-12-30 15:06:56 +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
|
|
|
}
|
|
|
|
|
2020-01-01 10:15:05 +01:00
|
|
|
pub fn parse_file(path string, table &table.Table) ast.File {
|
2020-02-19 16:12:39 +01:00
|
|
|
// println('parse_file("$path")')
|
2020-01-01 10:15:05 +01:00
|
|
|
text := os.read_file(path) or {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-12-28 14:11:05 +01:00
|
|
|
mut stmts := []ast.Stmt
|
2019-12-26 11:27:35 +01:00
|
|
|
mut p := Parser{
|
2019-12-28 14:11:05 +01:00
|
|
|
scanner: scanner.new_scanner(text)
|
2019-12-26 11:27:35 +01:00
|
|
|
table: table
|
2020-01-01 10:15:05 +01:00
|
|
|
file_name: path
|
2020-02-03 07:02:54 +01:00
|
|
|
pref: &pref.Preferences{}
|
2020-02-16 11:48:29 +01:00
|
|
|
scope: &ast.Scope{
|
|
|
|
start_pos: 0
|
|
|
|
parent: 0
|
|
|
|
}
|
2019-12-26 11:27:35 +01:00
|
|
|
}
|
2019-12-28 14:11:05 +01:00
|
|
|
p.read_first_token()
|
2020-02-16 11:48:29 +01:00
|
|
|
// p.scope = &ast.Scope{start_pos: p.tok.position(), parent: 0}
|
2020-01-18 23:26:14 +01:00
|
|
|
// module decl
|
2020-01-22 21:34:38 +01:00
|
|
|
module_decl := if p.tok.kind == .key_module { p.module_decl() } else { ast.Module{name: 'main'
|
|
|
|
} }
|
2020-02-08 09:50:12 +01:00
|
|
|
p.mod = module_decl.name
|
|
|
|
p.builtin_mod = p.mod == 'builtin'
|
2020-01-18 23:26:14 +01:00
|
|
|
// imports
|
|
|
|
mut imports := []ast.Import
|
|
|
|
for p.tok.kind == .key_import {
|
|
|
|
imports << p.import_stmt()
|
|
|
|
}
|
|
|
|
// TODO: import only mode
|
2019-12-26 11:27:35 +01:00
|
|
|
for {
|
2019-12-28 09:43:22 +01:00
|
|
|
// res := s.scan()
|
2019-12-28 09:15:32 +01:00
|
|
|
if p.tok.kind == .eof {
|
2020-02-19 16:12:39 +01:00
|
|
|
// println('EOF, breaking')
|
2019-12-26 11:27:35 +01:00
|
|
|
break
|
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
// println('stmt at ' + p.tok.str())
|
|
|
|
stmts << p.top_stmt()
|
2019-12-26 11:27:35 +01:00
|
|
|
}
|
2019-12-29 08:51:55 +01:00
|
|
|
// println('nr stmts = $stmts.len')
|
2019-12-28 14:11:05 +01:00
|
|
|
// println(stmts[0])
|
2020-02-15 13:37:48 +01:00
|
|
|
p.scope.end_pos = p.tok.pos
|
2019-12-30 12:10:46 +01:00
|
|
|
return ast.File{
|
2020-02-03 07:31:54 +01:00
|
|
|
path: path
|
2020-01-22 21:34:38 +01:00
|
|
|
mod: module_decl
|
2020-01-18 23:26:14 +01:00
|
|
|
imports: imports
|
2020-01-22 21:34:38 +01:00
|
|
|
stmts: stmts
|
2020-02-20 11:13:18 +01:00
|
|
|
scope: p.scope
|
2019-12-28 14:11:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-01-02 08:37:41 +01:00
|
|
|
files << parse_file(path, table)
|
2019-12-30 12:10:46 +01:00
|
|
|
}
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2020-01-07 01:57:38 +01:00
|
|
|
pub fn (p &Parser) init_parse_fns() {
|
|
|
|
// p.prefix_parse_fns = make(100, 100, sizeof(PrefixParseFn))
|
2020-01-06 16:13:12 +01:00
|
|
|
// p.prefix_parse_fns[token.Kind.name] = parse_name
|
2020-01-07 01:57:38 +01:00
|
|
|
println('')
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
|
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-26 11:27:35 +01:00
|
|
|
}
|
|
|
|
|
2020-02-15 13:37:48 +01:00
|
|
|
pub fn (p mut Parser) open_scope() {
|
|
|
|
p.scope = &ast.Scope{
|
|
|
|
parent: p.scope
|
|
|
|
start_pos: p.tok.pos
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (p mut Parser) close_scope() {
|
|
|
|
p.scope.end_pos = p.tok.pos
|
|
|
|
p.scope.parent.children << p.scope
|
|
|
|
p.scope = p.scope.parent
|
|
|
|
}
|
|
|
|
|
2019-12-28 14:11:05 +01:00
|
|
|
pub fn (p mut Parser) parse_block() []ast.Stmt {
|
2020-02-15 13:37:48 +01:00
|
|
|
p.open_scope()
|
2019-12-31 19:42:16 +01:00
|
|
|
p.check(.lcbr)
|
2019-12-28 14:11:05 +01:00
|
|
|
mut stmts := []ast.Stmt
|
2020-01-06 16:13:12 +01:00
|
|
|
if p.tok.kind != .rcbr {
|
|
|
|
for {
|
|
|
|
stmts << p.stmt()
|
|
|
|
// p.warn('after stmt(): tok=$p.tok.str()')
|
|
|
|
if p.tok.kind in [.eof, .rcbr] {
|
|
|
|
break
|
|
|
|
}
|
2019-12-27 13:57:49 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-31 19:42:16 +01:00
|
|
|
p.check(.rcbr)
|
2020-02-18 17:29:47 +01:00
|
|
|
// println('parse block')
|
2020-02-15 13:37:48 +01:00
|
|
|
p.close_scope()
|
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() {
|
2019-12-28 09:15:32 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-31 10:53:30 +01:00
|
|
|
fn (p mut Parser) check(expected token.Kind) {
|
2019-12-28 09:15:32 +01:00
|
|
|
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 {
|
2019-12-28 09:15:32 +01:00
|
|
|
name := p.tok.lit
|
2019-12-27 13:57:49 +01:00
|
|
|
p.check(.name)
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2020-01-06 16:13:12 +01:00
|
|
|
pub fn (p mut Parser) top_stmt() ast.Stmt {
|
2019-12-28 09:15:32 +01:00
|
|
|
match p.tok.kind {
|
2019-12-31 19:42:16 +01:00
|
|
|
.key_pub {
|
|
|
|
match p.peek_tok.kind {
|
2020-01-18 23:26:14 +01:00
|
|
|
.key_const {
|
|
|
|
return p.const_decl()
|
|
|
|
}
|
2019-12-31 19:42:16 +01:00
|
|
|
.key_fn {
|
|
|
|
return p.fn_decl()
|
|
|
|
}
|
|
|
|
.key_struct, .key_union, .key_interface {
|
|
|
|
return p.struct_decl()
|
|
|
|
}
|
2020-02-10 23:19:50 +01:00
|
|
|
.key_enum {
|
|
|
|
return p.enum_decl()
|
|
|
|
}
|
|
|
|
.key_type {
|
|
|
|
return p.type_decl()
|
|
|
|
}
|
2019-12-31 19:42:16 +01:00
|
|
|
else {
|
|
|
|
p.error('wrong pub keyword usage')
|
|
|
|
return ast.Stmt{}
|
|
|
|
}
|
2020-02-10 23:19:50 +01:00
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2020-02-03 07:02:54 +01:00
|
|
|
.lsbr {
|
|
|
|
return p.attr()
|
|
|
|
}
|
|
|
|
.key_global {
|
|
|
|
return p.global_decl()
|
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
.key_const {
|
|
|
|
return p.const_decl()
|
2019-12-31 19:42:16 +01:00
|
|
|
}
|
2019-12-28 09:43:22 +01:00
|
|
|
.key_fn {
|
|
|
|
return p.fn_decl()
|
|
|
|
}
|
2019-12-30 06:16:59 +01:00
|
|
|
.key_struct {
|
|
|
|
return p.struct_decl()
|
|
|
|
}
|
2020-02-03 07:02:54 +01:00
|
|
|
.dollar {
|
|
|
|
return p.comp_if()
|
2020-01-07 13:10:05 +01:00
|
|
|
}
|
2020-02-04 09:54:15 +01:00
|
|
|
.hash {
|
|
|
|
return p.hash()
|
|
|
|
}
|
2020-02-12 01:16:38 +01:00
|
|
|
.key_type {
|
|
|
|
return p.type_decl()
|
|
|
|
}
|
|
|
|
.key_enum {
|
|
|
|
return p.enum_decl()
|
|
|
|
}
|
2020-02-26 15:51:05 +01:00
|
|
|
.key_union {
|
|
|
|
return p.struct_decl()
|
|
|
|
}
|
2020-02-18 20:20:15 +01:00
|
|
|
.line_comment {
|
|
|
|
// p.next()
|
|
|
|
return ast.LineComment{
|
|
|
|
text: p.scanner.line_comment
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.mline_comment {
|
|
|
|
// p.next()
|
|
|
|
return ast.MultiLineComment{
|
|
|
|
text: p.scanner.line_comment
|
|
|
|
}
|
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
else {
|
2020-02-18 20:20:15 +01:00
|
|
|
// #printf("");
|
2020-02-26 15:51:05 +01:00
|
|
|
p.error('parser: bad top level statement ' + p.tok.str())
|
2020-02-03 07:02:54 +01:00
|
|
|
return ast.Stmt{}
|
2019-12-28 09:43:22 +01:00
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (p mut Parser) stmt() ast.Stmt {
|
|
|
|
match p.tok.kind {
|
2020-02-26 15:51:05 +01:00
|
|
|
.key_assert {
|
|
|
|
p.next()
|
|
|
|
expr,_ := p.expr(0)
|
|
|
|
return ast.AssertStmt{
|
|
|
|
expr: expr
|
|
|
|
}
|
|
|
|
}
|
2019-12-28 11:02:06 +01:00
|
|
|
.key_mut {
|
2020-02-28 13:29:04 +01:00
|
|
|
return p.var_decl_and_assign_stmt()
|
2019-12-28 11:02:06 +01:00
|
|
|
}
|
2019-12-30 06:16:59 +01:00
|
|
|
.key_for {
|
|
|
|
return p.for_statement()
|
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
.key_return {
|
|
|
|
return p.return_stmt()
|
|
|
|
}
|
2020-02-03 07:02:54 +01:00
|
|
|
.dollar {
|
|
|
|
return p.comp_if()
|
|
|
|
}
|
2020-02-04 08:29:50 +01:00
|
|
|
.key_continue, .key_break {
|
|
|
|
tok := p.tok
|
|
|
|
p.next()
|
|
|
|
return ast.BranchStmt{
|
2020-02-18 03:28:39 +01:00
|
|
|
tok: tok
|
2020-02-04 08:29:50 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-05 10:00:11 +01:00
|
|
|
.key_unsafe {
|
|
|
|
p.next()
|
2020-02-26 20:45:03 +01:00
|
|
|
stmts := p.parse_block()
|
|
|
|
return ast.UnsafeStmt{
|
|
|
|
stmts: stmts
|
|
|
|
}
|
2020-02-05 10:00:11 +01:00
|
|
|
}
|
2020-02-11 10:26:46 +01:00
|
|
|
.key_defer {
|
|
|
|
p.next()
|
|
|
|
stmts := p.parse_block()
|
|
|
|
return ast.DeferStmt{
|
|
|
|
stmts: stmts
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 14:15:42 +01:00
|
|
|
.key_goto {
|
|
|
|
p.next()
|
|
|
|
name := p.check_name()
|
|
|
|
return ast.GotoStmt{
|
|
|
|
name: name
|
|
|
|
}
|
|
|
|
}
|
2019-12-28 14:11:05 +01:00
|
|
|
else {
|
2020-01-06 16:13:12 +01:00
|
|
|
// `x := ...`
|
2020-02-28 13:29:04 +01:00
|
|
|
if p.tok.kind == .name && p.peek_tok.kind in [.decl_assign, .comma] {
|
|
|
|
return p.var_decl_and_assign_stmt()
|
2020-02-06 17:38:02 +01:00
|
|
|
}
|
2020-02-17 14:15:42 +01:00
|
|
|
// `label:`
|
|
|
|
else if p.tok.kind == .name && p.peek_tok.kind == .colon {
|
|
|
|
name := p.check_name()
|
|
|
|
p.check(.colon)
|
|
|
|
return ast.GotoLabel{
|
|
|
|
name: name
|
|
|
|
}
|
|
|
|
}
|
2020-02-16 11:48:29 +01:00
|
|
|
// expr,typ := p.expr(0)
|
2020-02-15 13:37:48 +01:00
|
|
|
expr,_ := p.expr(0)
|
2019-12-28 14:11:05 +01:00
|
|
|
return ast.ExprStmt{
|
|
|
|
expr: expr
|
2020-02-16 11:48:29 +01:00
|
|
|
// typ: typ
|
2020-02-28 15:36:41 +01:00
|
|
|
|
2019-12-28 14:11:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 13:29:04 +01:00
|
|
|
// TODO: merge wtih AssignStmt & VarDecl
|
2020-01-06 16:13:12 +01:00
|
|
|
pub fn (p mut Parser) assign_expr(left ast.Expr) ast.AssignExpr {
|
|
|
|
op := p.tok.kind
|
|
|
|
p.next()
|
2020-01-22 21:34:38 +01:00
|
|
|
val,_ := p.expr(0)
|
2020-01-06 16:13:12 +01:00
|
|
|
node := ast.AssignExpr{
|
|
|
|
left: left
|
|
|
|
val: val
|
2020-01-18 23:26:14 +01:00
|
|
|
op: op
|
|
|
|
pos: p.tok.position()
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2020-02-03 07:02:54 +01:00
|
|
|
fn (p mut Parser) attr() ast.Attr {
|
|
|
|
p.check(.lsbr)
|
2020-02-18 17:29:47 +01:00
|
|
|
if p.tok.kind == .key_if {
|
|
|
|
p.next()
|
|
|
|
}
|
2020-02-03 07:02:54 +01:00
|
|
|
name := p.check_name()
|
|
|
|
p.check(.rsbr)
|
|
|
|
return ast.Attr{
|
|
|
|
name: name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 23:19:50 +01:00
|
|
|
/*
|
|
|
|
|
2020-02-02 14:31:54 +01:00
|
|
|
fn (p mut Parser) range_expr(low ast.Expr) ast.Expr {
|
|
|
|
// ,table.Type) {
|
2020-02-05 10:00:11 +01:00
|
|
|
if p.tok.kind != .dotdot {
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
p.check(.dotdot)
|
2020-02-07 07:34:18 +01:00
|
|
|
mut high := ast.Expr{}
|
|
|
|
if p.tok.kind != .rsbr {
|
|
|
|
high,_ = p.expr(0)
|
|
|
|
// if typ.typ.kind != .int {
|
|
|
|
// p.error('non-integer index `$typ.typ.name`')
|
|
|
|
// }
|
2020-02-02 14:31:54 +01:00
|
|
|
}
|
|
|
|
node := ast.RangeExpr{
|
|
|
|
low: low
|
|
|
|
high: high
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
}
|
2020-02-10 23:19:50 +01:00
|
|
|
*/
|
2020-02-02 14:31:54 +01:00
|
|
|
|
2020-01-06 16:13:12 +01:00
|
|
|
/*
|
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 {
|
2020-01-06 16:13:12 +01:00
|
|
|
p.error('assign unknown variable `$name`')
|
2019-12-29 06:50:08 +01:00
|
|
|
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)
|
2020-01-18 23:26:14 +01:00
|
|
|
if !p.table.check(left_type, right_type) {
|
2019-12-29 08:51:55 +01:00
|
|
|
p.error('oops')
|
|
|
|
}
|
2019-12-28 19:16:04 +01:00
|
|
|
return ast.AssignStmt{
|
|
|
|
left: left_expr
|
|
|
|
right: right_expr
|
|
|
|
op: op
|
|
|
|
}
|
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
*/
|
|
|
|
|
2019-12-28 19:16:04 +01:00
|
|
|
|
2019-12-29 06:50:08 +01:00
|
|
|
pub fn (p &Parser) error(s string) {
|
2020-01-07 01:57:38 +01:00
|
|
|
print_backtrace()
|
2020-02-07 07:34:18 +01:00
|
|
|
mut path := p.file_name
|
|
|
|
// Get relative path
|
2020-02-17 20:31:23 +01:00
|
|
|
workdir := os.getwd() + filepath.separator
|
2020-02-07 07:34:18 +01:00
|
|
|
if path.starts_with(workdir) {
|
|
|
|
path = path.replace(workdir, '')
|
|
|
|
}
|
2020-02-11 10:26:46 +01:00
|
|
|
final_msg_line := '$path:$p.tok.line_nr: error: $s'
|
2020-01-09 14:08:33 +01:00
|
|
|
if colored_output {
|
|
|
|
eprintln(term.bold(term.red(final_msg_line)))
|
2020-01-22 21:34:38 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-01-09 14:08:33 +01:00
|
|
|
eprintln(final_msg_line)
|
|
|
|
}
|
2019-12-29 06:50:08 +01:00
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
|
2020-01-02 08:30:15 +01:00
|
|
|
pub fn (p &Parser) error_at_line(s string, line_nr int) {
|
2020-01-09 14:08:33 +01:00
|
|
|
final_msg_line := '$p.file_name:$line_nr: error: $s'
|
|
|
|
if colored_output {
|
|
|
|
eprintln(term.bold(term.red(final_msg_line)))
|
2020-01-22 21:34:38 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-01-09 14:08:33 +01:00
|
|
|
eprintln(final_msg_line)
|
|
|
|
}
|
2020-01-02 08:30:15 +01:00
|
|
|
exit(1)
|
2019-12-30 09:38:12 +01:00
|
|
|
}
|
|
|
|
|
2020-01-02 08:30:15 +01:00
|
|
|
pub fn (p &Parser) warn(s string) {
|
2020-01-09 14:08:33 +01:00
|
|
|
final_msg_line := '$p.file_name:$p.tok.line_nr: warning: $s'
|
|
|
|
if colored_output {
|
|
|
|
eprintln(term.bold(term.blue(final_msg_line)))
|
2020-01-22 21:34:38 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-01-09 14:08:33 +01:00
|
|
|
eprintln(final_msg_line)
|
|
|
|
}
|
2019-12-29 08:51:55 +01:00
|
|
|
}
|
|
|
|
|
2020-02-17 12:25:18 +01:00
|
|
|
pub fn (p mut Parser) parse_ident(is_c bool) ast.Ident {
|
2020-02-06 17:38:02 +01:00
|
|
|
// p.warn('name ')
|
2020-02-28 13:29:04 +01:00
|
|
|
mut name := p.check_name()
|
2020-02-27 11:12:30 +01:00
|
|
|
if name == '_' {
|
|
|
|
return ast.Ident{
|
2020-02-28 13:29:04 +01:00
|
|
|
name: '_'
|
2020-02-27 11:12:30 +01:00
|
|
|
kind: .blank_ident
|
|
|
|
pos: p.tok.position()
|
|
|
|
}
|
|
|
|
}
|
2020-02-28 13:29:04 +01:00
|
|
|
if p.expr_mod.len > 0 {
|
|
|
|
name = '${p.expr_mod}.$name'
|
|
|
|
}
|
2020-02-06 17:38:02 +01:00
|
|
|
mut ident := ast.Ident{
|
2020-02-28 13:29:04 +01:00
|
|
|
kind: .unresolved
|
2020-02-06 17:38:02 +01:00
|
|
|
name: name
|
2020-02-10 08:32:08 +01:00
|
|
|
is_c: is_c
|
2020-02-15 13:37:48 +01:00
|
|
|
pos: p.tok.position()
|
2020-02-06 17:38:02 +01:00
|
|
|
}
|
|
|
|
// variable
|
2020-02-28 13:29:04 +01:00
|
|
|
if p.expr_mod.len == 0 {
|
|
|
|
if var := p.scope.find_var(name) {
|
|
|
|
ident.kind = .variable
|
|
|
|
ident.info = ast.IdentVar{}
|
2020-02-22 14:39:25 +01:00
|
|
|
}
|
2020-02-06 17:38:02 +01:00
|
|
|
}
|
2020-02-28 13:29:04 +01:00
|
|
|
// handle consts/fns in checker
|
|
|
|
return ident
|
2020-02-06 17:38:02 +01:00
|
|
|
}
|
|
|
|
|
2020-02-18 08:58:20 +01:00
|
|
|
fn (p mut Parser) struct_init() ast.StructInit {
|
2020-02-12 17:39:35 +01:00
|
|
|
typ := p.parse_type()
|
2020-02-13 01:06:34 +01:00
|
|
|
sym := p.table.get_type_symbol(typ)
|
2020-02-18 20:20:15 +01:00
|
|
|
// p.warn('struct init typ=$sym.name')
|
2020-02-12 17:39:35 +01:00
|
|
|
p.check(.lcbr)
|
|
|
|
mut field_names := []string
|
|
|
|
mut exprs := []ast.Expr
|
|
|
|
mut i := 0
|
2020-02-26 15:51:05 +01:00
|
|
|
// TODO `if sym.info is table.Struct`
|
2020-02-12 17:39:35 +01:00
|
|
|
mut is_struct := false
|
|
|
|
match sym.info {
|
|
|
|
table.Struct {
|
|
|
|
is_struct = true
|
|
|
|
}
|
|
|
|
else {}
|
|
|
|
}
|
2020-02-26 15:51:05 +01:00
|
|
|
is_short_syntax := !(p.peek_tok.kind == .colon || p.tok.kind == .rcbr) // `Vec{a,b,c}`
|
|
|
|
// p.warn(is_short_syntax.str())
|
2020-02-12 17:39:35 +01:00
|
|
|
for p.tok.kind != .rcbr {
|
2020-02-26 15:51:05 +01:00
|
|
|
mut field_name := ''
|
|
|
|
if is_short_syntax {
|
|
|
|
expr,_ := p.expr(0)
|
|
|
|
exprs << expr
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
field_name = p.check_name()
|
|
|
|
field_names << field_name
|
|
|
|
}
|
2020-02-12 17:39:35 +01:00
|
|
|
// Set expected type for this field's expression
|
|
|
|
// p.warn('$sym.name field $field_name')
|
|
|
|
if is_struct {
|
|
|
|
info := sym.info as table.Struct
|
2020-02-26 15:51:05 +01:00
|
|
|
if is_short_syntax {}
|
|
|
|
else {
|
|
|
|
field := sym.find_field(field_name) or {
|
|
|
|
p.error('field `${sym.name}.$field_name` not found')
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
p.expected_type = field.typ
|
2020-02-12 17:39:35 +01:00
|
|
|
}
|
|
|
|
// p.warn('setting exp $field.typ')
|
|
|
|
}
|
2020-02-26 15:51:05 +01:00
|
|
|
if !is_short_syntax {
|
|
|
|
p.check(.colon)
|
|
|
|
expr,_ := p.expr(0)
|
|
|
|
exprs << expr
|
|
|
|
}
|
2020-02-12 17:39:35 +01:00
|
|
|
i++
|
2020-02-26 15:51:05 +01:00
|
|
|
if p.tok.kind == .comma {
|
|
|
|
p.check(.comma)
|
|
|
|
}
|
2020-02-12 17:39:35 +01:00
|
|
|
}
|
2020-02-18 08:58:20 +01:00
|
|
|
node := ast.StructInit{
|
2020-02-12 17:39:35 +01:00
|
|
|
typ: typ
|
|
|
|
exprs: exprs
|
|
|
|
fields: field_names
|
|
|
|
pos: p.tok.position()
|
|
|
|
}
|
|
|
|
p.check(.rcbr)
|
2020-02-18 08:58:20 +01:00
|
|
|
return node
|
2020-02-12 17:39:35 +01:00
|
|
|
}
|
|
|
|
|
2020-02-18 08:58:20 +01:00
|
|
|
pub fn (p mut Parser) name_expr() ast.Expr {
|
2020-01-06 16:13:12 +01:00
|
|
|
mut node := ast.Expr{}
|
2020-02-19 07:16:38 +01:00
|
|
|
is_c := p.tok.lit == 'C'
|
|
|
|
mut mod := ''
|
2020-02-27 00:12:37 +01:00
|
|
|
// p.warn('resetting')
|
|
|
|
p.expr_mod = ''
|
|
|
|
// `map[string]int` initialization
|
|
|
|
if p.tok.lit == 'map' && p.peek_tok.kind == .lsbr {
|
|
|
|
map_type := p.parse_map_type()
|
|
|
|
return node
|
|
|
|
}
|
2020-02-28 13:29:04 +01:00
|
|
|
if p.peek_tok.kind == .dot && (is_c || p.known_import(p.tok.lit) || p.mod.all_after('.') == p.tok.lit) {
|
2020-02-19 07:16:38 +01:00
|
|
|
if !is_c {
|
2020-02-20 11:13:18 +01:00
|
|
|
// prepend the full import
|
|
|
|
mod = p.imports[p.tok.lit]
|
2020-02-19 07:16:38 +01:00
|
|
|
}
|
2020-01-07 16:06:37 +01:00
|
|
|
p.next()
|
|
|
|
p.check(.dot)
|
2020-02-27 00:12:37 +01:00
|
|
|
p.expr_mod = mod
|
2020-02-05 10:00:11 +01:00
|
|
|
}
|
2020-02-18 17:29:47 +01:00
|
|
|
// p.warn('name expr $p.tok.lit $p.peek_tok.str()')
|
2020-02-07 21:29:28 +01:00
|
|
|
// fn call or type cast
|
2020-01-06 16:13:12 +01:00
|
|
|
if p.peek_tok.kind == .lpar {
|
2020-02-07 21:29:28 +01:00
|
|
|
name := p.tok.lit
|
|
|
|
// type cast. TODO: finish
|
2020-02-10 20:33:34 +01:00
|
|
|
// if name in table.builtin_type_names {
|
2020-02-11 10:26:46 +01:00
|
|
|
if name in p.table.type_idxs && !(name in ['stat', 'sigaction']) {
|
|
|
|
// TODO handle C.stat()
|
2020-02-10 14:43:17 +01:00
|
|
|
to_typ := p.parse_type()
|
|
|
|
p.check(.lpar)
|
|
|
|
mut expr := ast.Expr{}
|
2020-02-10 20:33:34 +01:00
|
|
|
expr,_ = p.expr(0)
|
|
|
|
// TODO, string(b, len)
|
2020-02-16 16:16:19 +01:00
|
|
|
if p.tok.kind == .comma && table.type_idx(to_typ) == table.string_type_idx {
|
2020-02-10 14:43:17 +01:00
|
|
|
p.check(.comma)
|
|
|
|
p.expr(0) // len
|
2020-02-07 21:29:28 +01:00
|
|
|
}
|
2020-02-10 14:43:17 +01:00
|
|
|
p.check(.rpar)
|
|
|
|
node = ast.CastExpr{
|
|
|
|
typ: to_typ
|
|
|
|
expr: expr
|
|
|
|
}
|
2020-02-27 00:12:37 +01:00
|
|
|
p.expr_mod = ''
|
2020-02-18 08:58:20 +01:00
|
|
|
return node
|
2020-02-07 21:29:28 +01:00
|
|
|
}
|
2020-02-10 14:43:17 +01:00
|
|
|
// fn call
|
2020-02-07 21:29:28 +01:00
|
|
|
else {
|
2020-02-18 08:58:20 +01:00
|
|
|
// println('calling $p.tok.lit')
|
2020-02-19 07:16:38 +01:00
|
|
|
x := p.call_expr(is_c, mod) // TODO `node,typ :=` should work
|
2020-02-07 21:29:28 +01:00
|
|
|
node = x
|
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
2020-02-18 17:29:47 +01:00
|
|
|
else if p.peek_tok.kind == .lcbr && (p.tok.lit[0].is_capital() || is_c ||
|
|
|
|
//
|
2020-02-19 07:16:38 +01:00
|
|
|
p.tok.lit in table.builtin_type_names) &&
|
2020-02-18 17:29:47 +01:00
|
|
|
//
|
|
|
|
(p.tok.lit.len == 1 || !p.tok.lit[p.tok.lit.len - 1].is_capital())
|
|
|
|
//
|
|
|
|
{
|
2020-02-05 10:00:11 +01:00
|
|
|
// || p.table.known_type(p.tok.lit)) {
|
2020-02-12 17:39:35 +01:00
|
|
|
return p.struct_init()
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
2020-02-25 15:02:34 +01:00
|
|
|
else if p.peek_tok.kind == .dot && p.tok.lit[0].is_capital() {
|
2020-02-27 00:12:37 +01:00
|
|
|
// `Color.green`
|
|
|
|
mut enum_name := p.check_name()
|
|
|
|
if mod != '' {
|
|
|
|
enum_name = mod + '.' + enum_name
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
enum_name = p.prepend_mod(enum_name)
|
|
|
|
}
|
|
|
|
// p.warn('Color.green $enum_name ' + p.prepend_mod(enum_name) + 'mod=$mod')
|
2020-02-25 15:02:34 +01:00
|
|
|
p.check(.dot)
|
|
|
|
val := p.check_name()
|
|
|
|
// println('enum val $enum_name . $val')
|
2020-02-27 00:12:37 +01:00
|
|
|
p.expr_mod = ''
|
2020-02-25 15:02:34 +01:00
|
|
|
return ast.EnumVal{
|
2020-02-27 00:12:37 +01:00
|
|
|
enum_name: enum_name // lp.prepend_mod(enum_name)
|
2020-02-28 15:36:41 +01:00
|
|
|
|
2020-02-25 15:02:34 +01:00
|
|
|
val: val
|
2020-02-27 00:12:37 +01:00
|
|
|
pos: p.tok.position()
|
2020-02-25 15:02:34 +01:00
|
|
|
}
|
2020-02-25 13:30:43 +01:00
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
else {
|
2020-02-06 17:38:02 +01:00
|
|
|
mut ident := ast.Ident{}
|
2020-02-17 12:25:18 +01:00
|
|
|
ident = p.parse_ident(is_c)
|
2020-02-06 17:38:02 +01:00
|
|
|
node = ident
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
2020-02-27 00:12:37 +01:00
|
|
|
p.expr_mod = ''
|
2020-02-18 08:58:20 +01:00
|
|
|
return node
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 08:32:08 +01:00
|
|
|
pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
|
2020-01-22 21:34:38 +01:00
|
|
|
// println('\n\nparser.expr()')
|
2020-02-10 08:32:08 +01:00
|
|
|
mut typ := table.void_type
|
2019-12-28 14:11:05 +01:00
|
|
|
mut node := ast.Expr{}
|
2020-01-06 16:13:12 +01:00
|
|
|
// Prefix
|
2019-12-28 14:11:05 +01:00
|
|
|
match p.tok.kind {
|
2019-12-28 09:15:32 +01:00
|
|
|
.name {
|
2020-02-18 08:58:20 +01:00
|
|
|
node = p.name_expr()
|
2019-12-28 09:15:32 +01:00
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
.str {
|
2020-01-22 21:34:38 +01:00
|
|
|
node,typ = p.string_expr()
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
2020-02-10 20:33:34 +01:00
|
|
|
.dot {
|
|
|
|
// .enum_val
|
2020-02-26 15:51:05 +01:00
|
|
|
// node,typ = p.enum_val()
|
|
|
|
node = p.enum_val()
|
2020-02-10 20:33:34 +01:00
|
|
|
}
|
2020-02-04 09:54:15 +01:00
|
|
|
.chartoken {
|
2020-02-10 08:32:08 +01:00
|
|
|
typ = table.byte_type
|
2020-02-04 09:54:15 +01:00
|
|
|
node = ast.CharLiteral{
|
|
|
|
val: p.tok.lit
|
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
}
|
2020-02-05 10:00:11 +01:00
|
|
|
// -1, -a, !x, &x, ~x
|
|
|
|
.minus, .amp, .mul, .not, .bit_not {
|
2020-02-18 08:58:20 +01:00
|
|
|
node = p.prefix_expr()
|
2019-12-30 09:38:12 +01:00
|
|
|
}
|
2020-01-07 12:14:10 +01:00
|
|
|
// .amp {
|
|
|
|
// p.next()
|
|
|
|
// }
|
2019-12-30 06:16:59 +01:00
|
|
|
.key_true, .key_false {
|
2019-12-29 08:51:55 +01:00
|
|
|
node = ast.BoolLiteral{
|
2019-12-30 06:16:59 +01:00
|
|
|
val: p.tok.kind == .key_true
|
2019-12-29 08:51:55 +01:00
|
|
|
}
|
2020-02-10 08:32:08 +01:00
|
|
|
typ = table.bool_type
|
2019-12-29 08:51:55 +01:00
|
|
|
p.next()
|
|
|
|
}
|
2020-02-05 10:00:11 +01:00
|
|
|
.key_match {
|
2020-02-16 11:48:29 +01:00
|
|
|
// node,typ = p.match_expr()
|
2020-02-15 13:37:48 +01:00
|
|
|
node = p.match_expr()
|
2020-02-05 10:00:11 +01:00
|
|
|
}
|
2019-12-28 09:43:22 +01:00
|
|
|
.number {
|
2020-02-18 17:29:47 +01:00
|
|
|
node,typ = p.parse_number_literal()
|
2019-12-28 09:43:22 +01:00
|
|
|
}
|
2019-12-25 13:39:58 +01:00
|
|
|
.lpar {
|
2019-12-31 10:53:30 +01:00
|
|
|
p.check(.lpar)
|
2020-01-22 21:34:38 +01:00
|
|
|
node,typ = p.expr(0)
|
2019-12-28 11:02:06 +01:00
|
|
|
p.check(.rpar)
|
2020-02-28 14:41:19 +01:00
|
|
|
node = ast.ParExpr{
|
|
|
|
expr: node
|
|
|
|
}
|
2019-12-27 13:57:49 +01:00
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
.key_if {
|
2020-02-15 13:37:48 +01:00
|
|
|
node = p.if_expr()
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
.lsbr {
|
2020-02-15 13:37:48 +01:00
|
|
|
node = p.array_init()
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
2020-02-05 10:00:11 +01:00
|
|
|
.key_none {
|
|
|
|
p.next()
|
2020-02-19 19:54:36 +01:00
|
|
|
typ = table.none_type
|
2020-02-20 15:42:56 +01:00
|
|
|
node = ast.None{}
|
2020-02-05 10:00:11 +01:00
|
|
|
}
|
2020-02-04 09:54:15 +01:00
|
|
|
.key_sizeof {
|
2020-02-18 18:13:34 +01:00
|
|
|
p.next() // sizeof
|
2020-02-04 09:54:15 +01:00
|
|
|
p.check(.lpar)
|
2020-02-19 16:12:39 +01:00
|
|
|
if p.tok.lit == 'C' {
|
|
|
|
p.next()
|
|
|
|
p.check(.dot)
|
|
|
|
}
|
|
|
|
if p.tok.kind == .amp {
|
|
|
|
p.next()
|
|
|
|
}
|
2020-02-18 20:20:15 +01:00
|
|
|
type_name := p.check_name()
|
2020-02-04 09:54:15 +01:00
|
|
|
p.check(.rpar)
|
2020-02-18 20:20:15 +01:00
|
|
|
node = ast.SizeOf{
|
|
|
|
type_name: type_name
|
|
|
|
}
|
2020-02-10 08:32:08 +01:00
|
|
|
typ = table.int_type
|
2020-02-04 09:54:15 +01:00
|
|
|
}
|
2020-02-17 14:15:42 +01:00
|
|
|
// Map `{"age": 20}` or `{ x | foo:bar, a:10 }`
|
2020-02-11 00:07:01 +01:00
|
|
|
.lcbr {
|
|
|
|
p.next()
|
2020-02-17 14:15:42 +01:00
|
|
|
if p.tok.kind == .str {
|
2020-02-22 14:13:19 +01:00
|
|
|
mut keys := []ast.Expr
|
|
|
|
mut vals := []ast.Expr
|
2020-02-17 14:15:42 +01:00
|
|
|
for p.tok.kind != .rcbr && p.tok.kind != .eof {
|
2020-02-22 14:39:25 +01:00
|
|
|
// p.check(.str)
|
|
|
|
key,_ := p.expr(0)
|
|
|
|
keys << key
|
2020-02-17 14:15:42 +01:00
|
|
|
p.check(.colon)
|
2020-02-22 14:39:25 +01:00
|
|
|
val,_ := p.expr(0)
|
2020-02-22 14:13:19 +01:00
|
|
|
vals << val
|
2020-02-17 14:15:42 +01:00
|
|
|
if p.tok.kind == .comma {
|
|
|
|
p.next()
|
|
|
|
}
|
2020-02-11 00:07:01 +01:00
|
|
|
}
|
2020-02-22 14:39:25 +01:00
|
|
|
node = ast.MapInit{
|
|
|
|
keys: keys
|
2020-02-22 14:13:19 +01:00
|
|
|
vals: vals
|
|
|
|
pos: p.tok.position()
|
|
|
|
}
|
2020-02-17 14:15:42 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-02-18 17:29:47 +01:00
|
|
|
name := p.check_name()
|
2020-02-22 14:39:25 +01:00
|
|
|
mut fields := []string
|
|
|
|
mut vals := []ast.Expr
|
2020-02-17 14:15:42 +01:00
|
|
|
p.check(.pipe)
|
|
|
|
for {
|
2020-02-22 14:39:25 +01:00
|
|
|
fields << p.check_name()
|
2020-02-17 14:15:42 +01:00
|
|
|
p.check(.colon)
|
2020-02-22 14:39:25 +01:00
|
|
|
expr,_ := p.expr(0)
|
|
|
|
vals << expr
|
2020-02-17 14:15:42 +01:00
|
|
|
if p.tok.kind == .comma {
|
|
|
|
p.check(.comma)
|
|
|
|
}
|
|
|
|
if p.tok.kind == .rcbr {
|
|
|
|
break
|
|
|
|
}
|
2020-02-11 00:07:01 +01:00
|
|
|
}
|
2020-02-18 17:29:47 +01:00
|
|
|
node = ast.Assoc{
|
|
|
|
name: name
|
2020-02-22 14:39:25 +01:00
|
|
|
fields: fields
|
|
|
|
exprs: vals
|
2020-02-18 17:29:47 +01:00
|
|
|
}
|
2020-02-11 00:07:01 +01:00
|
|
|
}
|
|
|
|
p.check(.rcbr)
|
|
|
|
}
|
2019-12-27 13:57:49 +01:00
|
|
|
else {
|
2020-02-28 14:05:20 +01:00
|
|
|
p.error('parser: expr(): bad token `$p.tok.str()`')
|
2019-12-26 11:27:35 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
// Infix
|
|
|
|
for precedence < p.tok.precedence() {
|
|
|
|
if p.tok.kind.is_assign() {
|
|
|
|
node = p.assign_expr(node)
|
|
|
|
}
|
|
|
|
else if p.tok.kind == .dot {
|
2020-02-15 13:37:48 +01:00
|
|
|
node = p.dot_expr(node, typ)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
2020-01-07 12:14:10 +01:00
|
|
|
else if p.tok.kind == .lsbr {
|
2020-02-20 15:42:56 +01:00
|
|
|
node = p.index_expr(node)
|
2020-01-07 12:14:10 +01:00
|
|
|
}
|
2020-02-07 09:19:45 +01:00
|
|
|
else if p.tok.kind == .key_as {
|
|
|
|
p.next()
|
|
|
|
typ = p.parse_type()
|
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
else if p.tok.kind.is_infix() {
|
2020-01-22 21:34:38 +01:00
|
|
|
node,typ = p.infix_expr(node)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
2020-01-07 12:14:10 +01:00
|
|
|
// Postfix
|
2020-01-06 16:13:12 +01:00
|
|
|
else if p.tok.kind in [.inc, .dec] {
|
|
|
|
node = ast.PostfixExpr{
|
|
|
|
op: p.tok.kind
|
|
|
|
expr: node
|
2020-02-27 17:31:10 +01:00
|
|
|
pos: p.tok.position()
|
2019-12-24 18:54:43 +01:00
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
p.next()
|
2020-01-22 21:34:38 +01:00
|
|
|
return node,typ
|
2019-12-25 13:39:58 +01:00
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
else {
|
2020-01-22 21:34:38 +01:00
|
|
|
return node,typ
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-22 21:34:38 +01:00
|
|
|
return node,typ
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
|
2020-02-18 08:58:20 +01:00
|
|
|
fn (p mut Parser) prefix_expr() ast.PrefixExpr {
|
2020-01-06 16:13:12 +01:00
|
|
|
op := p.tok.kind
|
|
|
|
p.next()
|
2020-02-18 08:58:20 +01:00
|
|
|
right,_ := p.expr(1)
|
|
|
|
return ast.PrefixExpr{
|
2020-01-06 16:13:12 +01:00
|
|
|
op: op
|
|
|
|
right: right
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 13:37:48 +01:00
|
|
|
fn (p mut Parser) index_expr(left ast.Expr) ast.IndexExpr {
|
2020-02-10 23:19:50 +01:00
|
|
|
// left == `a` in `a[0]`
|
2020-02-02 14:31:54 +01:00
|
|
|
p.next() // [
|
2020-02-10 23:19:50 +01:00
|
|
|
if p.tok.kind == .dotdot {
|
|
|
|
// [..end]
|
|
|
|
p.next()
|
|
|
|
high,_ := p.expr(0)
|
|
|
|
p.check(.rsbr)
|
|
|
|
return ast.IndexExpr{
|
|
|
|
left: left
|
|
|
|
pos: p.tok.position()
|
|
|
|
index: ast.RangeExpr{
|
|
|
|
low: ast.Expr{}
|
|
|
|
high: high
|
|
|
|
}
|
2020-02-15 13:37:48 +01:00
|
|
|
}
|
2020-02-02 14:31:54 +01:00
|
|
|
}
|
2020-02-10 23:19:50 +01:00
|
|
|
expr,_ := p.expr(0) // `[expr]` or `[expr..]`
|
|
|
|
if p.tok.kind == .dotdot {
|
|
|
|
// [start..end] or [start..]
|
|
|
|
p.check(.dotdot)
|
|
|
|
mut high := ast.Expr{}
|
|
|
|
if p.tok.kind != .rsbr {
|
|
|
|
high,_ = p.expr(0)
|
|
|
|
}
|
|
|
|
p.check(.rsbr)
|
|
|
|
return ast.IndexExpr{
|
|
|
|
left: left
|
|
|
|
pos: p.tok.position()
|
|
|
|
index: ast.RangeExpr{
|
|
|
|
low: expr
|
|
|
|
high: high
|
|
|
|
}
|
2020-02-15 13:37:48 +01:00
|
|
|
}
|
2020-02-11 12:59:40 +01:00
|
|
|
}
|
|
|
|
// get the element type
|
2020-02-15 13:37:48 +01:00
|
|
|
/*
|
2020-02-11 12:59:40 +01:00
|
|
|
mut typ := left_type
|
|
|
|
left_type_sym := p.table.get_type_symbol(left_type)
|
|
|
|
if left_type_sym.kind == .array {
|
|
|
|
info := left_type_sym.info as table.Array
|
|
|
|
typ = info.elem_type
|
2020-02-02 14:31:54 +01:00
|
|
|
}
|
2020-02-15 13:37:48 +01:00
|
|
|
*/
|
2020-02-10 23:19:50 +01:00
|
|
|
// [expr]
|
2020-01-07 12:14:10 +01:00
|
|
|
p.check(.rsbr)
|
2020-02-10 23:19:50 +01:00
|
|
|
return ast.IndexExpr{
|
2020-01-07 12:14:10 +01:00
|
|
|
left: left
|
2020-02-10 23:19:50 +01:00
|
|
|
index: expr
|
2020-02-03 11:29:50 +01:00
|
|
|
pos: p.tok.position()
|
2020-02-15 13:37:48 +01:00
|
|
|
}
|
2020-01-07 12:14:10 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 14:42:57 +01:00
|
|
|
fn (p mut Parser) filter(typ table.Type) {
|
2020-02-15 13:37:48 +01:00
|
|
|
p.scope.register_var(ast.VarDecl{
|
|
|
|
name: 'it'
|
|
|
|
typ: typ
|
|
|
|
})
|
2020-02-10 14:42:57 +01:00
|
|
|
}
|
|
|
|
|
2020-02-15 13:37:48 +01:00
|
|
|
fn (p mut Parser) dot_expr(left ast.Expr, left_type table.Type) ast.Expr {
|
2020-01-06 16:13:12 +01:00
|
|
|
p.next()
|
|
|
|
field_name := p.check_name()
|
2020-02-10 14:42:57 +01:00
|
|
|
if field_name == 'filter' {
|
2020-02-15 13:37:48 +01:00
|
|
|
p.open_scope()
|
2020-02-10 14:42:57 +01:00
|
|
|
p.filter(left_type)
|
2020-02-27 07:00:24 +01:00
|
|
|
defer {
|
|
|
|
p.close_scope()
|
|
|
|
}
|
2020-02-10 14:42:57 +01:00
|
|
|
}
|
2020-01-07 01:08:24 +01:00
|
|
|
// Method call
|
|
|
|
if p.tok.kind == .lpar {
|
|
|
|
p.next()
|
2020-02-27 21:00:33 +01:00
|
|
|
args,muts := p.call_args()
|
2020-02-28 15:36:41 +01:00
|
|
|
mut or_stmts := []ast.Stmt
|
2020-02-04 17:44:39 +01:00
|
|
|
if p.tok.kind == .key_orelse {
|
|
|
|
p.next()
|
2020-02-28 15:36:41 +01:00
|
|
|
or_stmts = p.parse_block()
|
2020-02-04 17:44:39 +01:00
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
mcall_expr := ast.MethodCallExpr{
|
2020-01-07 01:08:24 +01:00
|
|
|
expr: left
|
|
|
|
name: field_name
|
|
|
|
args: args
|
2020-02-27 21:51:40 +01:00
|
|
|
muts: muts
|
2020-01-18 23:26:14 +01:00
|
|
|
pos: p.tok.position()
|
2020-02-28 15:36:41 +01:00
|
|
|
or_block: ast.OrExpr{
|
|
|
|
stmts: or_stmts
|
|
|
|
}
|
2020-01-07 01:08:24 +01:00
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
mut node := ast.Expr{}
|
|
|
|
node = mcall_expr
|
2020-02-15 13:37:48 +01:00
|
|
|
return node
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
sel_expr := ast.SelectorExpr{
|
2020-01-06 16:13:12 +01:00
|
|
|
expr: left
|
|
|
|
field: field_name
|
2020-01-18 23:26:14 +01:00
|
|
|
pos: p.tok.position()
|
2019-12-22 02:34:37 +01:00
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
mut node := ast.Expr{}
|
|
|
|
node = sel_expr
|
2020-02-15 13:37:48 +01:00
|
|
|
return node
|
2019-12-24 18:54:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 08:32:08 +01:00
|
|
|
fn (p mut Parser) infix_expr(left ast.Expr) (ast.Expr,table.Type) {
|
2020-01-06 16:13:12 +01:00
|
|
|
op := p.tok.kind
|
|
|
|
// mut typ := p.
|
|
|
|
// println('infix op=$op.str()')
|
|
|
|
precedence := p.tok.precedence()
|
2020-02-19 11:31:33 +01:00
|
|
|
pos := p.tok.position()
|
2020-01-06 16:13:12 +01:00
|
|
|
p.next()
|
2020-02-17 14:15:42 +01:00
|
|
|
mut typ := table.Type{}
|
|
|
|
mut right := ast.Expr{}
|
|
|
|
right,typ = p.expr(precedence)
|
2020-01-06 16:13:12 +01:00
|
|
|
if op.is_relational() {
|
2020-02-10 08:32:08 +01:00
|
|
|
typ = table.bool_type
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
mut expr := ast.Expr{}
|
2020-01-22 21:34:38 +01:00
|
|
|
expr = ast.InfixExpr{
|
2020-01-06 16:13:12 +01:00
|
|
|
left: left
|
|
|
|
right: right
|
2020-02-06 13:57:35 +01:00
|
|
|
right_type: typ
|
2020-01-18 23:26:14 +01:00
|
|
|
op: op
|
2020-02-19 11:31:33 +01:00
|
|
|
pos: pos
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
return expr,typ
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implementation of Pratt Precedence
|
2020-02-04 08:29:50 +01:00
|
|
|
/*
|
2020-01-03 11:36:17 +01:00
|
|
|
[inline]
|
|
|
|
fn (p &Parser) is_addative() bool {
|
|
|
|
return p.tok.kind in [.plus, .minus] && p.peek_tok.kind in [.number, .name]
|
|
|
|
}
|
2020-02-04 08:29:50 +01:00
|
|
|
*/
|
2020-02-10 20:33:34 +01:00
|
|
|
// `.green`
|
2020-02-20 15:42:56 +01:00
|
|
|
// `pref.BuildMode.default_mode`
|
2020-02-26 15:51:05 +01:00
|
|
|
fn (p mut Parser) enum_val() ast.EnumVal {
|
2020-02-10 20:33:34 +01:00
|
|
|
p.check(.dot)
|
2020-02-25 15:02:34 +01:00
|
|
|
val := p.check_name()
|
2020-02-26 15:51:05 +01:00
|
|
|
/*
|
|
|
|
if p.expected_type == 0 {
|
|
|
|
p.error('wtf')
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
sym := p.table.get_type_symbol(p.expected_type) //or {
|
|
|
|
//return ast.EnumVal{val:val}
|
|
|
|
//}
|
|
|
|
p.warn(sym.name)
|
|
|
|
*/
|
|
|
|
|
|
|
|
return ast.EnumVal{
|
2020-02-25 15:02:34 +01:00
|
|
|
val: val
|
2020-02-27 00:12:37 +01:00
|
|
|
pos: p.tok.position()
|
2020-02-10 20:33:34 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-03 11:36:17 +01:00
|
|
|
|
2020-01-07 00:14:19 +01:00
|
|
|
fn (p mut Parser) for_statement() ast.Stmt {
|
2019-12-30 06:16:59 +01:00
|
|
|
p.check(.key_for)
|
2020-02-15 13:37:48 +01:00
|
|
|
p.open_scope()
|
2020-02-18 03:17:21 +01:00
|
|
|
// defer { p.close_scope() }
|
2020-01-07 00:14:19 +01:00
|
|
|
// Infinite loop
|
|
|
|
if p.tok.kind == .lcbr {
|
2020-02-19 11:06:36 +01:00
|
|
|
pos := p.tok.position()
|
2020-01-07 00:14:19 +01:00
|
|
|
stmts := p.parse_block()
|
2020-02-15 13:37:48 +01:00
|
|
|
p.close_scope()
|
2020-01-07 00:14:19 +01:00
|
|
|
return ast.ForStmt{
|
|
|
|
stmts: stmts
|
2020-02-19 11:06:36 +01:00
|
|
|
pos: pos
|
|
|
|
is_inf: true
|
2020-01-07 00:14:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if p.tok.kind == .key_mut {
|
|
|
|
p.error('`mut` is not required in for loops')
|
|
|
|
}
|
|
|
|
// for i := 0; i < 10; i++ {
|
2020-02-07 07:34:18 +01:00
|
|
|
else if p.peek_tok.kind in [.decl_assign, .assign, .semicolon] || p.tok.kind == .semicolon {
|
2020-01-07 00:14:19 +01:00
|
|
|
mut init := ast.Stmt{}
|
|
|
|
mut cond := ast.Expr{}
|
|
|
|
mut inc := ast.Stmt{}
|
2020-02-28 13:29:04 +01:00
|
|
|
if p.peek_tok.kind in [.assign, .decl_assign] {
|
|
|
|
init = p.var_decl_and_assign_stmt()
|
2020-02-20 20:30:34 +01:00
|
|
|
}
|
2020-02-19 16:12:39 +01:00
|
|
|
else if p.tok.kind != .semicolon {}
|
|
|
|
// allow `for ;; i++ {`
|
|
|
|
// Allow `for i = 0; i < ...`
|
|
|
|
/*
|
2020-01-07 00:14:19 +01:00
|
|
|
cond, typ = p.expr(0)
|
|
|
|
if typ.kind != _bool {
|
|
|
|
p.error('non-bool used as for condition')
|
|
|
|
}
|
|
|
|
*/
|
2020-02-19 16:12:39 +01:00
|
|
|
// println(1)
|
|
|
|
// }
|
2020-01-07 00:14:19 +01:00
|
|
|
p.check(.semicolon)
|
|
|
|
if p.tok.kind != .semicolon {
|
2020-02-10 08:32:08 +01:00
|
|
|
mut typ := table.void_type
|
2020-01-07 00:14:19 +01:00
|
|
|
cond,typ = p.expr(0)
|
|
|
|
}
|
|
|
|
p.check(.semicolon)
|
|
|
|
if p.tok.kind != .lcbr {
|
|
|
|
inc = p.stmt()
|
|
|
|
}
|
|
|
|
stmts := p.parse_block()
|
2020-02-15 13:37:48 +01:00
|
|
|
p.close_scope()
|
2020-01-07 00:14:19 +01:00
|
|
|
return ast.ForCStmt{
|
|
|
|
stmts: stmts
|
|
|
|
init: init
|
|
|
|
cond: cond
|
|
|
|
inc: inc
|
|
|
|
}
|
|
|
|
}
|
2020-02-04 08:29:50 +01:00
|
|
|
// `for i in vals`, `for i in start .. end`
|
2020-02-11 12:59:40 +01:00
|
|
|
else if p.peek_tok.kind in [.key_in, .comma] {
|
2020-02-11 13:21:41 +01:00
|
|
|
var_name := p.check_name()
|
2020-02-27 18:02:40 +01:00
|
|
|
mut val_name := ''
|
2020-02-04 17:44:39 +01:00
|
|
|
if p.tok.kind == .comma {
|
|
|
|
p.check(.comma)
|
2020-02-27 18:02:40 +01:00
|
|
|
val_name = p.check_name()
|
2020-02-16 11:48:29 +01:00
|
|
|
// p.table.register_var(table.Var{
|
|
|
|
// name: val_name
|
|
|
|
// typ: table.int_type
|
|
|
|
// })
|
2020-02-15 13:37:48 +01:00
|
|
|
p.scope.register_var(ast.VarDecl{
|
2020-02-04 17:44:39 +01:00
|
|
|
name: val_name
|
2020-02-10 08:32:08 +01:00
|
|
|
typ: table.int_type
|
2020-02-04 17:44:39 +01:00
|
|
|
})
|
|
|
|
}
|
2020-01-01 22:34:46 +01:00
|
|
|
p.check(.key_in)
|
2020-02-10 14:43:17 +01:00
|
|
|
mut elem_type := table.void_type
|
2020-02-10 20:33:34 +01:00
|
|
|
// arr_expr
|
2020-02-22 16:59:50 +01:00
|
|
|
cond,arr_typ := p.expr(0)
|
2020-02-10 14:43:17 +01:00
|
|
|
// array / map
|
2020-02-11 12:59:40 +01:00
|
|
|
if table.type_idx(arr_typ) == table.string_type_idx {
|
|
|
|
elem_type = table.byte_type
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
arr_typ_sym := p.table.get_type_symbol(arr_typ)
|
|
|
|
match arr_typ_sym.info {
|
|
|
|
table.Array {
|
|
|
|
elem_type = it.elem_type
|
|
|
|
}
|
|
|
|
table.Map {
|
|
|
|
elem_type = it.value_type
|
|
|
|
}
|
2020-02-19 16:12:39 +01:00
|
|
|
else {}
|
|
|
|
// println(1)
|
|
|
|
// elem_type_sym := p.table.get_type_symbol(elem_type)
|
|
|
|
// p.error('cannot loop over type: $elem_type_sym.name')
|
|
|
|
// }
|
|
|
|
}
|
2020-02-11 12:59:40 +01:00
|
|
|
}
|
2020-02-10 14:43:17 +01:00
|
|
|
// 0 .. 10
|
|
|
|
// start := p.tok.lit.int()
|
2020-02-28 15:36:41 +01:00
|
|
|
// TODO use RangeExpr
|
|
|
|
mut high_expr := ast.Expr{}
|
|
|
|
mut is_range := false
|
2020-02-04 08:29:50 +01:00
|
|
|
if p.tok.kind == .dotdot {
|
2020-02-28 15:36:41 +01:00
|
|
|
is_range = true
|
2020-02-04 08:29:50 +01:00
|
|
|
p.check(.dotdot)
|
2020-02-28 15:36:41 +01:00
|
|
|
high_expr,_ = p.expr(0)
|
2020-02-04 08:29:50 +01:00
|
|
|
}
|
2020-02-16 11:48:29 +01:00
|
|
|
// p.table.register_var(table.Var{
|
|
|
|
// name: var_name
|
|
|
|
// typ: elem_type
|
|
|
|
// })
|
2020-02-15 13:37:48 +01:00
|
|
|
p.scope.register_var(ast.VarDecl{
|
2020-02-04 08:29:50 +01:00
|
|
|
name: var_name
|
2020-02-10 14:43:17 +01:00
|
|
|
typ: elem_type
|
2020-02-04 08:29:50 +01:00
|
|
|
})
|
2020-01-01 22:34:46 +01:00
|
|
|
stmts := p.parse_block()
|
|
|
|
// println('nr stmts=$stmts.len')
|
2020-02-15 13:37:48 +01:00
|
|
|
p.close_scope()
|
2020-02-18 20:47:19 +01:00
|
|
|
return ast.ForInStmt{
|
2020-01-01 22:34:46 +01:00
|
|
|
stmts: stmts
|
2020-02-03 07:02:54 +01:00
|
|
|
pos: p.tok.position()
|
2020-02-22 16:59:50 +01:00
|
|
|
cond: cond
|
2020-02-27 18:02:40 +01:00
|
|
|
key_var: var_name
|
|
|
|
val_var: val_name
|
2020-02-28 15:36:41 +01:00
|
|
|
high: high_expr
|
|
|
|
is_range: is_range
|
2020-01-01 22:34:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// `for cond {`
|
2020-02-03 12:06:25 +01:00
|
|
|
cond,_ := p.expr(0)
|
2019-12-30 06:16:59 +01:00
|
|
|
stmts := p.parse_block()
|
2020-02-15 13:37:48 +01:00
|
|
|
p.close_scope()
|
2019-12-30 06:16:59 +01:00
|
|
|
return ast.ForStmt{
|
|
|
|
cond: cond
|
|
|
|
stmts: stmts
|
2020-02-03 07:02:54 +01:00
|
|
|
pos: p.tok.position()
|
2019-12-30 06:16:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 13:37:48 +01:00
|
|
|
fn (p mut Parser) if_expr() ast.Expr {
|
2020-01-07 01:57:38 +01:00
|
|
|
p.inside_if = true
|
|
|
|
// defer {
|
|
|
|
// }
|
2019-12-29 08:51:55 +01:00
|
|
|
mut node := ast.Expr{}
|
|
|
|
p.check(.key_if)
|
2020-02-19 11:31:33 +01:00
|
|
|
pos := p.tok.position()
|
2020-02-11 10:26:46 +01:00
|
|
|
// `if x := opt() {`
|
|
|
|
mut cond := ast.Expr{}
|
2020-02-27 18:02:40 +01:00
|
|
|
mut is_or := false
|
2020-02-11 10:26:46 +01:00
|
|
|
if p.peek_tok.kind == .decl_assign {
|
2020-02-27 18:02:40 +01:00
|
|
|
is_or = true
|
|
|
|
p.open_scope()
|
|
|
|
var_name := p.check_name()
|
|
|
|
// p.table.register_var(
|
2020-02-11 10:26:46 +01:00
|
|
|
p.check(.decl_assign)
|
2020-02-27 18:02:40 +01:00
|
|
|
expr,typ := p.expr(0)
|
|
|
|
p.scope.register_var(ast.VarDecl{
|
|
|
|
name: var_name
|
|
|
|
typ: typ
|
|
|
|
})
|
2020-02-28 15:36:41 +01:00
|
|
|
cond = ast.IfGuardExpr{
|
2020-02-27 18:02:40 +01:00
|
|
|
var_name: var_name
|
|
|
|
expr: expr
|
|
|
|
}
|
2020-02-11 10:26:46 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
cond,_ = p.expr(0)
|
|
|
|
}
|
2020-01-22 21:34:38 +01:00
|
|
|
p.inside_if = false
|
2020-02-22 16:59:50 +01:00
|
|
|
mut has_else := false
|
2019-12-29 08:51:55 +01:00
|
|
|
stmts := p.parse_block()
|
2019-12-31 19:42:16 +01:00
|
|
|
mut else_stmts := []ast.Stmt
|
|
|
|
if p.tok.kind == .key_else {
|
|
|
|
p.check(.key_else)
|
2020-02-04 17:44:39 +01:00
|
|
|
if p.tok.kind == .key_if {
|
2020-02-22 16:59:50 +01:00
|
|
|
// The next if block is handled by next if_expr()
|
|
|
|
has_else = true
|
2020-02-04 17:44:39 +01:00
|
|
|
}
|
2020-02-22 16:59:50 +01:00
|
|
|
// p.if_expr()
|
2020-02-04 17:44:39 +01:00
|
|
|
else {
|
|
|
|
else_stmts = p.parse_block()
|
|
|
|
}
|
2019-12-31 19:42:16 +01:00
|
|
|
}
|
2020-02-27 18:02:40 +01:00
|
|
|
if is_or {
|
|
|
|
p.close_scope()
|
|
|
|
}
|
2020-02-16 11:48:29 +01:00
|
|
|
// mut typ := table.void_type
|
2020-01-02 20:09:15 +01:00
|
|
|
// mut left := ast.Expr{}
|
2020-01-04 00:06:01 +01:00
|
|
|
// If the last statement is an expression, return its type
|
2020-02-15 13:37:48 +01:00
|
|
|
/*
|
2020-02-05 10:00:11 +01:00
|
|
|
if stmts.len > 0 {
|
|
|
|
match stmts[stmts.len - 1] {
|
|
|
|
ast.ExprStmt {
|
2020-02-10 08:32:08 +01:00
|
|
|
type_sym := p.table.get_type_symbol(it.typ)
|
|
|
|
p.warn('if expr ret $type_sym.name')
|
2020-02-06 13:57:35 +01:00
|
|
|
typ = it.typ
|
2020-02-05 10:00:11 +01:00
|
|
|
// return node,it.ti
|
|
|
|
// left =
|
|
|
|
}
|
|
|
|
else {}
|
|
|
|
}
|
2020-01-02 20:09:15 +01:00
|
|
|
}
|
2020-02-15 13:37:48 +01:00
|
|
|
*/
|
2020-02-16 11:48:29 +01:00
|
|
|
|
2019-12-29 08:51:55 +01:00
|
|
|
node = ast.IfExpr{
|
|
|
|
cond: cond
|
|
|
|
stmts: stmts
|
2019-12-31 19:42:16 +01:00
|
|
|
else_stmts: else_stmts
|
2020-02-16 11:48:29 +01:00
|
|
|
// typ: typ
|
2020-02-28 15:36:41 +01:00
|
|
|
|
2020-02-19 11:31:33 +01:00
|
|
|
pos: pos
|
2020-02-22 17:07:03 +01:00
|
|
|
has_else: has_else
|
2020-01-02 20:09:15 +01:00
|
|
|
// left: left
|
2020-02-28 15:36:41 +01:00
|
|
|
|
2019-12-29 08:51:55 +01:00
|
|
|
}
|
2020-02-15 13:37:48 +01:00
|
|
|
return node
|
2019-12-29 08:51:55 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 08:32:08 +01:00
|
|
|
fn (p mut Parser) string_expr() (ast.Expr,table.Type) {
|
2019-12-28 09:15:32 +01:00
|
|
|
mut node := ast.Expr{}
|
|
|
|
node = ast.StringLiteral{
|
|
|
|
val: p.tok.lit
|
|
|
|
}
|
2020-01-07 01:08:24 +01:00
|
|
|
if p.peek_tok.kind != .str_dollar {
|
|
|
|
p.next()
|
2020-02-10 08:32:08 +01:00
|
|
|
return node,table.string_type
|
2020-01-07 01:08:24 +01:00
|
|
|
}
|
|
|
|
// Handle $ interpolation
|
|
|
|
for p.tok.kind == .str {
|
|
|
|
p.next()
|
|
|
|
if p.tok.kind != .str_dollar {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
p.check(.str_dollar)
|
|
|
|
p.expr(0)
|
2020-02-10 20:33:34 +01:00
|
|
|
if p.tok.kind == .colon {
|
2020-02-04 09:54:15 +01:00
|
|
|
p.next()
|
|
|
|
}
|
2020-02-29 05:56:16 +01:00
|
|
|
// ${num:-2d}
|
|
|
|
if p.tok.kind == .minus {
|
|
|
|
p.next()
|
|
|
|
}
|
2020-02-11 10:26:46 +01:00
|
|
|
// ${num:2d}
|
|
|
|
if p.tok.kind == .number {
|
|
|
|
p.next()
|
|
|
|
if p.tok.lit.len == 1 {
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
}
|
2020-01-07 01:08:24 +01:00
|
|
|
}
|
2020-02-10 08:32:08 +01:00
|
|
|
return node,table.string_type
|
2019-12-28 09:15:32 +01:00
|
|
|
}
|
|
|
|
|
2020-02-19 14:34:44 +01:00
|
|
|
fn (p mut Parser) array_init() ast.ArrayInit {
|
2019-12-30 09:38:12 +01:00
|
|
|
p.check(.lsbr)
|
2020-02-12 01:16:38 +01:00
|
|
|
// `[]` - empty array with an automatically deduced type
|
2020-02-12 17:39:35 +01:00
|
|
|
// p.warn('array_init() exp=$p.expected_type')
|
2020-02-12 01:16:38 +01:00
|
|
|
/*
|
2020-02-12 17:39:35 +01:00
|
|
|
if p.expected_type != 0 {
|
|
|
|
sym := p.table.get_type_symbol(p.expected_type)
|
|
|
|
println(sym.name)
|
|
|
|
}
|
|
|
|
*/
|
2020-02-15 13:37:48 +01:00
|
|
|
/* TODO: joe
|
2020-02-12 17:39:35 +01:00
|
|
|
if p.tok.kind == .rsbr && int(p.expected_type) != 0 && p.table.get_type_symbol(p.expected_type).kind == .array {
|
|
|
|
// p.warn('[] expr')
|
2020-02-12 01:16:38 +01:00
|
|
|
node = ast.ArrayInit{
|
|
|
|
// typ: array_type
|
|
|
|
exprs: []
|
|
|
|
pos: p.tok.position()
|
|
|
|
}
|
2020-02-12 17:39:35 +01:00
|
|
|
p.check(.rsbr)
|
2020-02-12 01:16:38 +01:00
|
|
|
return node,p.expected_type
|
|
|
|
}
|
2020-02-15 13:37:48 +01:00
|
|
|
*/
|
2020-02-16 11:48:29 +01:00
|
|
|
|
2020-02-19 14:34:44 +01:00
|
|
|
mut array_type := table.Type(0)
|
2019-12-30 09:38:12 +01:00
|
|
|
mut exprs := []ast.Expr
|
2020-02-16 11:48:29 +01:00
|
|
|
// mut is_fixed := false
|
|
|
|
// mut fixed_size := 0
|
2020-02-10 14:43:17 +01:00
|
|
|
if p.tok.kind == .rsbr {
|
2020-02-17 14:15:42 +01:00
|
|
|
// []typ => `[]` and `typ` must be on the same line
|
|
|
|
line_nr := p.tok.line_nr
|
2020-02-10 14:43:17 +01:00
|
|
|
p.check(.rsbr)
|
|
|
|
// []string
|
2020-02-17 14:15:42 +01:00
|
|
|
if p.tok.kind == .name && p.tok.line_nr == line_nr {
|
2020-02-19 14:34:44 +01:00
|
|
|
val_type := p.parse_type()
|
|
|
|
// this is set here becasue its a known type, others could be the
|
|
|
|
// result of expr so we do those in checker
|
|
|
|
idx := p.table.find_or_register_array(val_type, 1)
|
|
|
|
array_type = table.new_type(idx)
|
2020-02-10 14:43:17 +01:00
|
|
|
}
|
|
|
|
// []
|
2020-02-19 16:12:39 +01:00
|
|
|
else {}
|
|
|
|
// TODO ?
|
2020-02-10 14:43:17 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// [1,2,3]
|
|
|
|
for i := 0; p.tok.kind != .rsbr; i++ {
|
2020-02-19 14:34:44 +01:00
|
|
|
expr,_ := p.expr(0)
|
2020-02-10 14:43:17 +01:00
|
|
|
exprs << expr
|
|
|
|
if p.tok.kind == .comma {
|
|
|
|
p.check(.comma)
|
|
|
|
}
|
|
|
|
}
|
2020-02-16 11:48:29 +01:00
|
|
|
// line_nr := p.tok.line_nr
|
2020-02-10 14:43:17 +01:00
|
|
|
p.check(.rsbr)
|
|
|
|
// Fixed size array? (`[100]byte`)
|
2020-02-15 13:37:48 +01:00
|
|
|
// NOTE: this should be hanled in parse_type() ?
|
|
|
|
/*
|
2020-02-10 14:43:17 +01:00
|
|
|
if exprs.len == 1 && p.tok.kind == .name && p.tok.line_nr == line_nr {
|
|
|
|
is_fixed = true
|
|
|
|
val_type = p.parse_type()
|
|
|
|
match exprs[0] {
|
2020-02-10 20:33:34 +01:00
|
|
|
ast.IntegerLiteral {
|
|
|
|
fixed_size = it.val
|
|
|
|
}
|
2020-02-10 14:43:17 +01:00
|
|
|
else {}
|
2020-02-15 13:37:48 +01:00
|
|
|
}
|
2020-02-10 14:43:17 +01:00
|
|
|
p.warn('fixed size array')
|
2019-12-30 09:38:12 +01:00
|
|
|
}
|
2020-02-15 13:37:48 +01:00
|
|
|
*/
|
2020-02-16 11:48:29 +01:00
|
|
|
|
2019-12-30 09:38:12 +01:00
|
|
|
}
|
2020-02-19 16:12:39 +01:00
|
|
|
// !
|
|
|
|
if p.tok.kind == .not {
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
if p.tok.kind == .not {
|
|
|
|
p.next()
|
|
|
|
}
|
2020-02-16 11:48:29 +01:00
|
|
|
// idx := if is_fixed { p.table.find_or_register_array_fixed(val_type, fixed_size, 1) } else { p.table.find_or_register_array(val_type, 1) }
|
|
|
|
// array_type := table.new_type(idx)
|
2020-02-19 14:34:44 +01:00
|
|
|
return ast.ArrayInit{
|
|
|
|
typ: array_type
|
2019-12-30 09:38:12 +01:00
|
|
|
exprs: exprs
|
2020-01-19 13:52:34 +01:00
|
|
|
pos: p.tok.position()
|
2019-12-30 09:38:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 08:32:08 +01:00
|
|
|
fn (p mut Parser) parse_number_literal() (ast.Expr,table.Type) {
|
2019-12-28 09:15:32 +01:00
|
|
|
lit := p.tok.lit
|
|
|
|
mut node := ast.Expr{}
|
2020-02-18 08:58:20 +01:00
|
|
|
mut typ := table.int_type
|
2019-12-28 09:15:32 +01:00
|
|
|
if lit.contains('.') {
|
|
|
|
node = ast.FloatLiteral{
|
2019-12-28 09:43:22 +01:00
|
|
|
// val: lit.f64()
|
2019-12-28 09:15:32 +01:00
|
|
|
val: lit
|
|
|
|
}
|
2020-02-18 08:58:20 +01:00
|
|
|
typ = table.f64_type
|
2019-12-28 09:43:22 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-12-28 09:15:32 +01:00
|
|
|
node = ast.IntegerLiteral{
|
|
|
|
val: lit.int()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.next()
|
2020-02-18 08:58:20 +01:00
|
|
|
return node,typ
|
2019-12-28 09:15:32 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 12:10:46 +01:00
|
|
|
fn (p mut Parser) module_decl() ast.Module {
|
|
|
|
p.check(.key_module)
|
2020-02-19 03:08:10 +01:00
|
|
|
mod := p.check_name()
|
|
|
|
full_mod := p.table.qualify_module(mod, p.file_name)
|
|
|
|
p.mod = full_mod
|
2020-01-18 23:26:14 +01:00
|
|
|
return ast.Module{
|
2020-02-19 03:08:10 +01:00
|
|
|
name: full_mod
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2019-12-28 09:43:22 +01:00
|
|
|
}
|
2019-12-28 09:15:32 +01:00
|
|
|
|
2020-01-18 23:26:14 +01:00
|
|
|
fn (p mut Parser) parse_import() ast.Import {
|
2020-02-12 01:16:38 +01:00
|
|
|
mut mod_name := p.check_name()
|
2020-01-18 23:26:14 +01:00
|
|
|
mut mod_alias := mod_name
|
2020-02-19 03:08:10 +01:00
|
|
|
for p.tok.kind == .dot {
|
|
|
|
p.check(.dot)
|
|
|
|
submod_name := p.check_name()
|
|
|
|
mod_name += '.' + submod_name
|
|
|
|
mod_alias = submod_name
|
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
if p.tok.kind == .key_as {
|
|
|
|
p.check(.key_as)
|
2020-01-18 23:26:14 +01:00
|
|
|
mod_alias = p.check_name()
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
2020-02-19 03:08:10 +01:00
|
|
|
p.imports[mod_alias] = mod_name
|
|
|
|
p.table.imports << mod_name
|
2019-12-31 19:42:16 +01:00
|
|
|
return ast.Import{
|
2020-01-18 23:26:14 +01:00
|
|
|
mod: mod_name
|
|
|
|
alias: mod_alias
|
|
|
|
pos: p.tok.position()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) import_stmt() []ast.Import {
|
|
|
|
p.check(.key_import)
|
|
|
|
mut imports := []ast.Import
|
|
|
|
if p.tok.kind == .lpar {
|
|
|
|
p.check(.lpar)
|
|
|
|
for p.tok.kind != .rpar {
|
|
|
|
imports << p.parse_import()
|
|
|
|
}
|
|
|
|
p.check(.rpar)
|
2020-01-22 21:34:38 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-01-18 23:26:14 +01:00
|
|
|
imports << p.parse_import()
|
|
|
|
}
|
|
|
|
return imports
|
|
|
|
}
|
|
|
|
|
2020-02-03 07:02:54 +01:00
|
|
|
fn (p mut Parser) const_decl() ast.ConstDecl {
|
2020-02-10 23:19:50 +01:00
|
|
|
is_pub := p.tok.kind == .key_pub
|
|
|
|
if is_pub {
|
|
|
|
p.next()
|
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
p.check(.key_const)
|
|
|
|
p.check(.lpar)
|
2020-02-03 07:02:54 +01:00
|
|
|
mut fields := []ast.Field
|
|
|
|
mut exprs := []ast.Expr
|
2020-01-18 23:26:14 +01:00
|
|
|
for p.tok.kind != .rpar {
|
2020-02-22 13:04:56 +01:00
|
|
|
name := p.prepend_mod(p.check_name())
|
2020-02-22 17:07:03 +01:00
|
|
|
// println('!!const: $name')
|
2020-01-18 23:26:14 +01:00
|
|
|
p.check(.assign)
|
2020-02-03 07:02:54 +01:00
|
|
|
expr,typ := p.expr(0)
|
|
|
|
fields << ast.Field{
|
|
|
|
name: name
|
|
|
|
typ: typ
|
|
|
|
}
|
|
|
|
exprs << expr
|
|
|
|
p.table.register_const(table.Var{
|
|
|
|
name: name
|
|
|
|
typ: typ
|
|
|
|
})
|
2019-12-31 19:42:16 +01:00
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
p.check(.rpar)
|
2020-02-03 07:02:54 +01:00
|
|
|
return ast.ConstDecl{
|
|
|
|
fields: fields
|
|
|
|
exprs: exprs
|
2020-02-26 22:43:37 +01:00
|
|
|
is_pub: is_pub
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
2019-12-28 09:15:32 +01:00
|
|
|
}
|
|
|
|
|
2020-02-26 15:51:05 +01:00
|
|
|
// structs and unions
|
2019-12-30 06:16:59 +01:00
|
|
|
fn (p mut Parser) struct_decl() ast.StructDecl {
|
2019-12-31 19:42:16 +01:00
|
|
|
is_pub := p.tok.kind == .key_pub
|
|
|
|
if is_pub {
|
|
|
|
p.next()
|
|
|
|
}
|
2020-02-26 15:51:05 +01:00
|
|
|
if p.tok.kind == .key_struct {
|
|
|
|
p.check(.key_struct)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.check(.key_union)
|
|
|
|
}
|
2020-02-10 23:19:50 +01:00
|
|
|
is_c := p.tok.lit == 'C' && p.peek_tok.kind == .dot
|
|
|
|
if is_c {
|
|
|
|
p.next() // C
|
|
|
|
p.next() // .
|
|
|
|
}
|
2019-12-30 06:16:59 +01:00
|
|
|
name := p.check_name()
|
|
|
|
p.check(.lcbr)
|
2020-01-02 20:09:15 +01:00
|
|
|
mut ast_fields := []ast.Field
|
2020-01-22 21:34:38 +01:00
|
|
|
mut fields := []table.Field
|
2020-02-22 14:13:19 +01:00
|
|
|
mut mut_pos := -1
|
|
|
|
mut pub_pos := -1
|
|
|
|
mut pub_mut_pos := -1
|
2019-12-30 06:16:59 +01:00
|
|
|
for p.tok.kind != .rcbr {
|
2019-12-31 19:42:16 +01:00
|
|
|
if p.tok.kind == .key_pub {
|
|
|
|
p.check(.key_pub)
|
2020-02-04 12:50:58 +01:00
|
|
|
if p.tok.kind == .key_mut {
|
|
|
|
p.check(.key_mut)
|
2020-02-22 14:13:19 +01:00
|
|
|
pub_mut_pos = fields.len
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pub_pos = fields.len
|
2020-02-04 12:50:58 +01:00
|
|
|
}
|
|
|
|
p.check(.colon)
|
|
|
|
}
|
|
|
|
else if p.tok.kind == .key_mut {
|
|
|
|
p.check(.key_mut)
|
2019-12-31 19:42:16 +01:00
|
|
|
p.check(.colon)
|
2020-02-22 14:13:19 +01:00
|
|
|
mut_pos = fields.len
|
2019-12-31 19:42:16 +01:00
|
|
|
}
|
2019-12-30 06:16:59 +01:00
|
|
|
field_name := p.check_name()
|
2020-02-04 12:50:58 +01:00
|
|
|
// p.warn('field $field_name')
|
2020-02-06 13:57:35 +01:00
|
|
|
typ := p.parse_type()
|
2020-02-05 10:00:11 +01:00
|
|
|
// Default value
|
|
|
|
if p.tok.kind == .assign {
|
|
|
|
p.next()
|
|
|
|
p.expr(0)
|
|
|
|
}
|
2020-01-02 20:09:15 +01:00
|
|
|
ast_fields << ast.Field{
|
2019-12-30 06:16:59 +01:00
|
|
|
name: field_name
|
2020-02-06 13:57:35 +01:00
|
|
|
typ: typ
|
2019-12-30 06:16:59 +01:00
|
|
|
}
|
2020-01-22 21:34:38 +01:00
|
|
|
fields << table.Field{
|
2020-01-02 20:09:15 +01:00
|
|
|
name: field_name
|
2020-02-06 13:57:35 +01:00
|
|
|
typ: typ
|
2020-01-02 20:09:15 +01:00
|
|
|
}
|
2020-02-03 09:11:10 +01:00
|
|
|
// println('struct field $ti.name $field_name')
|
2019-12-30 06:16:59 +01:00
|
|
|
}
|
|
|
|
p.check(.rcbr)
|
2020-02-10 08:32:08 +01:00
|
|
|
t := table.TypeSymbol{
|
2020-02-08 09:50:12 +01:00
|
|
|
kind: .struct_
|
2020-02-16 11:48:29 +01:00
|
|
|
name: p.prepend_mod(name)
|
2020-02-08 09:50:12 +01:00
|
|
|
info: table.Struct{
|
|
|
|
fields: fields
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2020-02-04 17:44:39 +01:00
|
|
|
}
|
2020-02-08 09:50:12 +01:00
|
|
|
mut ret := 0
|
|
|
|
if p.builtin_mod && t.name in table.builtin_type_names {
|
|
|
|
// this allows overiding the builtins type
|
|
|
|
// with the real struct type info parsed from builtin
|
2020-02-10 08:32:08 +01:00
|
|
|
ret = p.table.register_builtin_type_symbol(t)
|
2020-02-10 14:42:57 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-02-10 08:32:08 +01:00
|
|
|
ret = p.table.register_type_symbol(t)
|
2020-02-08 09:50:12 +01:00
|
|
|
}
|
|
|
|
if ret == -1 {
|
|
|
|
p.error('cannot register type `$name`, another type with this name exists')
|
|
|
|
}
|
2020-02-27 00:12:37 +01:00
|
|
|
p.expr_mod = ''
|
2019-12-30 06:16:59 +01:00
|
|
|
return ast.StructDecl{
|
|
|
|
name: name
|
2019-12-31 19:42:16 +01:00
|
|
|
is_pub: is_pub
|
2020-01-02 20:09:15 +01:00
|
|
|
fields: ast_fields
|
2020-01-18 23:26:14 +01:00
|
|
|
pos: p.tok.position()
|
2020-02-22 14:13:19 +01:00
|
|
|
mut_pos: mut_pos
|
|
|
|
pub_pos: pub_pos
|
|
|
|
pub_mut_pos: pub_mut_pos
|
2019-12-30 06:16:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 14:11:05 +01:00
|
|
|
fn (p mut Parser) return_stmt() ast.Return {
|
2019-12-28 09:15:32 +01:00
|
|
|
p.next()
|
2020-01-07 12:10:07 +01:00
|
|
|
// return expressions
|
|
|
|
mut exprs := []ast.Expr
|
|
|
|
// return type idents
|
2020-01-22 21:34:38 +01:00
|
|
|
// mut got_tis := []table.Type
|
2020-02-10 08:32:08 +01:00
|
|
|
if table.type_idx(p.return_type) == table.void_type_idx {
|
2020-01-22 21:34:38 +01:00
|
|
|
return ast.Return{
|
|
|
|
pos: p.tok.position()
|
|
|
|
}
|
|
|
|
}
|
2020-01-07 12:10:07 +01:00
|
|
|
for {
|
2020-01-18 23:26:14 +01:00
|
|
|
// expr,ti := p.expr(0)
|
2020-01-22 21:34:38 +01:00
|
|
|
expr,_ := p.expr(0)
|
2020-01-07 12:10:07 +01:00
|
|
|
exprs << expr
|
2020-01-18 23:26:14 +01:00
|
|
|
// got_tis << ti
|
2020-01-07 12:10:07 +01:00
|
|
|
if p.tok.kind == .comma {
|
|
|
|
p.check(.comma)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
stmt := ast.Return{
|
2020-02-06 13:57:35 +01:00
|
|
|
expected_type: p.return_type
|
2020-01-07 12:10:07 +01:00
|
|
|
exprs: exprs
|
2020-01-18 23:26:14 +01:00
|
|
|
pos: p.tok.position()
|
2019-12-28 09:43:22 +01:00
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
return stmt
|
2019-12-28 09:15:32 +01:00
|
|
|
}
|
|
|
|
|
2020-02-28 13:29:04 +01:00
|
|
|
// left hand side of `=` or `:=` in `a,b,c := 1,2,3`
|
|
|
|
fn (p mut Parser) parse_assign_lhs() []ast.Ident {
|
2020-02-06 17:38:02 +01:00
|
|
|
mut idents := []ast.Ident
|
|
|
|
for {
|
2020-02-28 13:29:04 +01:00
|
|
|
is_mut := p.tok.kind == .key_mut
|
|
|
|
if is_mut {
|
|
|
|
p.check(.key_mut)
|
|
|
|
}
|
|
|
|
is_static := p.tok.kind == .key_static
|
|
|
|
if is_static {
|
|
|
|
p.check(.key_static)
|
|
|
|
}
|
|
|
|
mut ident := p.parse_ident(false)
|
|
|
|
ident.info = ast.IdentVar{
|
|
|
|
is_mut: is_mut
|
|
|
|
is_static: is_static
|
|
|
|
}
|
2020-02-06 17:38:02 +01:00
|
|
|
idents << ident
|
|
|
|
if p.tok.kind == .comma {
|
|
|
|
p.check(.comma)
|
2020-02-07 07:34:18 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-02-06 17:38:02 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-02-28 13:29:04 +01:00
|
|
|
return idents
|
|
|
|
}
|
|
|
|
|
|
|
|
// right hand side of `=` or `:=` in `a,b,c := 1,2,3`
|
|
|
|
fn (p mut Parser) parse_assign_rhs() []ast.Expr {
|
|
|
|
mut exprs := []ast.Expr
|
|
|
|
for {
|
|
|
|
expr,_ := p.expr(0)
|
|
|
|
exprs << expr
|
|
|
|
if p.tok.kind == .comma {
|
|
|
|
p.check(.comma)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return exprs
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) var_decl_and_assign_stmt() ast.Stmt {
|
|
|
|
idents := p.parse_assign_lhs()
|
2020-02-23 11:22:07 +01:00
|
|
|
op := p.tok.kind
|
2020-02-06 17:38:02 +01:00
|
|
|
p.next() // :=, =
|
2020-02-28 13:29:04 +01:00
|
|
|
exprs := p.parse_assign_rhs()
|
2020-02-27 11:12:30 +01:00
|
|
|
is_decl := op == .decl_assign
|
2020-02-28 13:29:04 +01:00
|
|
|
// VarDecl
|
|
|
|
if idents.len == 1 {
|
|
|
|
ident := idents[0]
|
|
|
|
expr := exprs[0]
|
|
|
|
info0 := ident.var_info()
|
|
|
|
known_var := p.scope.known_var(ident.name)
|
|
|
|
if !is_decl && !known_var {
|
|
|
|
p.error('unknown variable `$ident.name`')
|
|
|
|
}
|
|
|
|
if is_decl && ident.kind != .blank_ident {
|
|
|
|
if known_var {
|
|
|
|
p.error('redefinition of `$ident.name`')
|
|
|
|
}
|
|
|
|
p.scope.register_var(ast.VarDecl{
|
|
|
|
name: ident.name
|
|
|
|
expr: expr
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return ast.VarDecl{
|
|
|
|
name: ident.name
|
|
|
|
// name2: name2
|
2020-02-28 15:36:41 +01:00
|
|
|
|
2020-02-28 13:29:04 +01:00
|
|
|
expr: expr // p.expr(token.lowest_prec)
|
2020-02-28 15:36:41 +01:00
|
|
|
|
2020-02-28 13:29:04 +01:00
|
|
|
is_mut: info0.is_mut
|
|
|
|
// typ: typ
|
2020-02-28 15:36:41 +01:00
|
|
|
|
2020-02-28 13:29:04 +01:00
|
|
|
pos: p.tok.position()
|
|
|
|
}
|
|
|
|
// return p.var_decl(ident[0], exprs[0])
|
|
|
|
}
|
|
|
|
// AssignStmt
|
2020-02-27 11:12:30 +01:00
|
|
|
for ident in idents {
|
2020-02-27 11:15:30 +01:00
|
|
|
if is_decl && ident.kind != .blank_ident {
|
2020-02-27 11:12:30 +01:00
|
|
|
if p.scope.known_var(ident.name) {
|
|
|
|
p.error('redefinition of `$ident.name`')
|
|
|
|
}
|
|
|
|
p.scope.register_var(ast.VarDecl{
|
|
|
|
name: ident.name
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 17:38:02 +01:00
|
|
|
return ast.AssignStmt{
|
|
|
|
left: idents
|
2020-02-28 13:29:04 +01:00
|
|
|
right: exprs
|
2020-02-23 11:22:07 +01:00
|
|
|
op: op
|
2020-02-27 11:12:30 +01:00
|
|
|
pos: p.tok.position()
|
2020-02-06 17:38:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 13:29:04 +01:00
|
|
|
// pub fn (p mut Parser) assign_stmt() ast.AssignStmt {}
|
|
|
|
// fn (p mut Parser) var_decl() ast.VarDecl {}
|
2020-02-04 09:54:15 +01:00
|
|
|
fn (p mut Parser) hash() ast.HashStmt {
|
|
|
|
p.next()
|
|
|
|
return ast.HashStmt{
|
|
|
|
name: p.tok.lit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 07:02:54 +01:00
|
|
|
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')
|
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
name := p.check_name()
|
|
|
|
println(name)
|
|
|
|
typ := p.parse_type()
|
|
|
|
if p.tok.kind == .assign {
|
|
|
|
p.next()
|
|
|
|
p.expr(0)
|
|
|
|
}
|
|
|
|
p.table.register_global(name, typ)
|
|
|
|
// p.genln(p.table.cgen_name_type_pair(name, typ))
|
|
|
|
/*
|
2020-02-10 20:33:34 +01:00
|
|
|
mut g := p.table.cgen_name_type_pair(name, typ)
|
|
|
|
if p.tok == .assign {
|
|
|
|
p.next()
|
|
|
|
g += ' = '
|
|
|
|
_,expr := p.tmp_expr()
|
|
|
|
g += expr
|
|
|
|
}
|
|
|
|
// p.genln('; // global')
|
|
|
|
g += '; // global'
|
|
|
|
if !p.cgen.nogen {
|
|
|
|
p.cgen.consts << g
|
|
|
|
}
|
|
|
|
*/
|
2020-02-03 07:02:54 +01:00
|
|
|
|
|
|
|
return ast.GlobalDecl{
|
|
|
|
name: name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 13:37:48 +01:00
|
|
|
fn (p mut Parser) match_expr() ast.Expr {
|
2020-02-05 10:00:11 +01:00
|
|
|
p.check(.key_match)
|
2020-02-12 01:16:38 +01:00
|
|
|
is_mut := p.tok.kind == .key_mut
|
|
|
|
if is_mut {
|
|
|
|
p.next()
|
|
|
|
}
|
2020-02-15 13:37:48 +01:00
|
|
|
cond,_ := p.expr(0)
|
2020-02-13 01:06:34 +01:00
|
|
|
// sym := p.table.get_type_symbol(typ)
|
|
|
|
// p.warn('match typ $sym.name')
|
2020-02-05 10:00:11 +01:00
|
|
|
p.check(.lcbr)
|
2020-02-07 14:49:14 +01:00
|
|
|
mut blocks := []ast.StmtBlock
|
|
|
|
mut match_exprs := []ast.Expr
|
2020-02-16 11:48:29 +01:00
|
|
|
// mut return_type := table.void_type
|
2020-02-05 10:00:11 +01:00
|
|
|
for {
|
2020-02-10 23:19:50 +01:00
|
|
|
// Sum type match
|
2020-02-13 01:06:34 +01:00
|
|
|
if p.tok.kind == .name && (p.tok.lit[0].is_capital() || p.peek_tok.kind == .dot) {
|
|
|
|
// if sym.kind == .sum_type {
|
|
|
|
// p.warn('is sum')
|
|
|
|
// p.parse_type()
|
2020-02-11 00:07:01 +01:00
|
|
|
p.check_name()
|
2020-02-13 01:06:34 +01:00
|
|
|
if p.tok.kind == .dot {
|
|
|
|
p.check(.dot)
|
|
|
|
p.check_name()
|
|
|
|
}
|
2020-02-11 00:07:01 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Expression match
|
2020-02-11 10:26:46 +01:00
|
|
|
for {
|
|
|
|
match_expr,_ := p.expr(0)
|
|
|
|
match_exprs << match_expr
|
|
|
|
if p.tok.kind != .comma {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
p.check(.comma)
|
|
|
|
}
|
2020-02-11 00:07:01 +01:00
|
|
|
}
|
2020-02-19 11:31:33 +01:00
|
|
|
// p.warn('match block')
|
2020-02-10 20:33:34 +01:00
|
|
|
stmts := p.parse_block()
|
2020-02-07 14:49:14 +01:00
|
|
|
blocks << ast.StmtBlock{
|
2020-02-10 20:33:34 +01:00
|
|
|
stmts: stmts
|
2020-02-07 14:49:14 +01:00
|
|
|
}
|
2020-02-05 10:00:11 +01:00
|
|
|
if p.tok.kind == .key_else {
|
|
|
|
p.next()
|
2020-02-07 14:49:14 +01:00
|
|
|
blocks << ast.StmtBlock{
|
|
|
|
stmts: p.parse_block()
|
|
|
|
}
|
2020-02-05 10:00:11 +01:00
|
|
|
}
|
2020-02-10 20:33:34 +01:00
|
|
|
// If the last statement is an expression, return its type
|
2020-02-15 13:37:48 +01:00
|
|
|
/*
|
2020-02-10 20:33:34 +01:00
|
|
|
if stmts.len > 0 {
|
|
|
|
match stmts[stmts.len - 1] {
|
|
|
|
ast.ExprStmt {
|
|
|
|
type_sym := p.table.get_type_symbol(it.typ)
|
|
|
|
p.warn('match expr ret $type_sym.name')
|
|
|
|
return_type = it.typ
|
|
|
|
}
|
|
|
|
else {}
|
2020-02-15 13:37:48 +01:00
|
|
|
}
|
2020-02-10 20:33:34 +01:00
|
|
|
}
|
2020-02-15 13:37:48 +01:00
|
|
|
*/
|
2020-02-16 11:48:29 +01:00
|
|
|
|
2020-02-05 10:00:11 +01:00
|
|
|
if p.tok.kind == .rcbr {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.check(.rcbr)
|
|
|
|
mut node := ast.Expr{}
|
2020-02-07 14:49:14 +01:00
|
|
|
node = ast.MatchExpr{
|
|
|
|
blocks: blocks
|
|
|
|
match_exprs: match_exprs
|
2020-02-16 11:48:29 +01:00
|
|
|
// typ: typ
|
2020-02-28 15:36:41 +01:00
|
|
|
|
2020-02-07 14:49:14 +01:00
|
|
|
cond: cond
|
|
|
|
}
|
2020-02-15 13:37:48 +01:00
|
|
|
return node
|
2020-02-16 11:48:29 +01:00
|
|
|
// return node,return_type
|
2020-02-05 10:00:11 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 23:19:50 +01:00
|
|
|
fn (p mut Parser) enum_decl() ast.EnumDecl {
|
|
|
|
is_pub := p.tok.kind == .key_pub
|
|
|
|
if is_pub {
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
p.check(.key_enum)
|
|
|
|
name := p.check_name()
|
|
|
|
p.check(.lcbr)
|
|
|
|
mut vals := []string
|
|
|
|
for p.tok.kind != .eof && p.tok.kind != .rcbr {
|
|
|
|
val := p.check_name()
|
|
|
|
vals << val
|
2020-02-19 11:31:33 +01:00
|
|
|
// p.warn('enum val $val')
|
2020-02-12 01:16:38 +01:00
|
|
|
if p.tok.kind == .assign {
|
|
|
|
p.next()
|
|
|
|
p.expr(0)
|
|
|
|
}
|
2020-02-10 23:19:50 +01:00
|
|
|
}
|
|
|
|
p.check(.rcbr)
|
2020-02-25 13:30:43 +01:00
|
|
|
p.table.register_type_symbol(table.TypeSymbol{
|
|
|
|
kind: .enum_
|
2020-02-27 00:12:37 +01:00
|
|
|
name: p.prepend_mod(name)
|
2020-02-25 13:30:43 +01:00
|
|
|
info: table.Enum{
|
|
|
|
vals: vals
|
|
|
|
}
|
|
|
|
})
|
2020-02-10 23:19:50 +01:00
|
|
|
return ast.EnumDecl{
|
|
|
|
name: name
|
|
|
|
is_pub: is_pub
|
|
|
|
vals: vals
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) type_decl() ast.TypeDecl {
|
|
|
|
is_pub := p.tok.kind == .key_pub
|
|
|
|
if is_pub {
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
p.check(.key_type)
|
|
|
|
name := p.check_name()
|
2020-02-13 01:06:34 +01:00
|
|
|
mut is_sum := false
|
2020-02-10 23:19:50 +01:00
|
|
|
// type SumType = A | B | c
|
|
|
|
if p.tok.kind == .assign {
|
|
|
|
p.next()
|
|
|
|
for {
|
|
|
|
p.check_name()
|
|
|
|
if p.tok.kind != .pipe {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
p.check(.pipe)
|
2020-02-13 01:06:34 +01:00
|
|
|
is_sum = true
|
2020-02-10 23:19:50 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-11 10:26:46 +01:00
|
|
|
else {
|
2020-02-27 00:18:14 +01:00
|
|
|
p.parse_type()
|
2020-02-11 10:26:46 +01:00
|
|
|
}
|
2020-02-13 01:06:34 +01:00
|
|
|
p.table.register_type_symbol(table.TypeSymbol{
|
|
|
|
kind: .sum_type
|
|
|
|
name: name
|
|
|
|
info: table.Alias{
|
|
|
|
foo: ''
|
|
|
|
}
|
|
|
|
})
|
2020-02-10 23:19:50 +01:00
|
|
|
return ast.TypeDecl{
|
|
|
|
name: name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 11:21:41 +01:00
|
|
|
fn verror(s string) {
|
|
|
|
println(s)
|
|
|
|
exit(1)
|
|
|
|
}
|