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
|
|
|
|
|
|
2020-04-14 19:32:23 +02:00
|
|
|
|
import v.scanner
|
|
|
|
|
import v.ast
|
|
|
|
|
import v.token
|
|
|
|
|
import v.table
|
|
|
|
|
import v.pref
|
|
|
|
|
import v.util
|
|
|
|
|
import term
|
|
|
|
|
import os
|
2020-01-22 21:34:38 +01:00
|
|
|
|
|
2019-12-22 02:34:37 +01:00
|
|
|
|
struct Parser {
|
2020-04-07 15:48:13 +02:00
|
|
|
|
scanner &scanner.Scanner
|
2020-04-10 12:01:06 +02:00
|
|
|
|
file_name string // "/home/user/hello.v"
|
|
|
|
|
file_name_dir string // "/home/user"
|
2019-12-22 02:34:37 +01:00
|
|
|
|
mut:
|
2020-04-07 15:48:13 +02:00
|
|
|
|
tok token.Token
|
|
|
|
|
peek_tok token.Token
|
|
|
|
|
table &table.Table
|
|
|
|
|
is_c bool
|
|
|
|
|
inside_if bool
|
2020-04-08 19:08:54 +02:00
|
|
|
|
inside_for bool
|
|
|
|
|
inside_fn bool
|
2020-04-07 15:48:13 +02:00
|
|
|
|
pref &pref.Preferences
|
|
|
|
|
builtin_mod bool
|
|
|
|
|
mod string
|
|
|
|
|
attr string
|
|
|
|
|
expr_mod string
|
|
|
|
|
scope &ast.Scope
|
|
|
|
|
global_scope &ast.Scope
|
|
|
|
|
imports map[string]string
|
|
|
|
|
ast_imports []ast.Import
|
|
|
|
|
is_amp bool
|
|
|
|
|
returns bool
|
2020-04-15 16:23:03 +02:00
|
|
|
|
inside_match bool // to separate `match A { }` from `Struct{}`
|
2020-04-05 03:37:38 +02:00
|
|
|
|
inside_match_case bool // to separate `match_expr { }` from `Struct{}`
|
2020-04-14 17:41:14 +02:00
|
|
|
|
is_stmt_ident bool // true while the beginning of a statement is an ident/selector
|
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 {
|
2020-02-29 17:51:35 +01:00
|
|
|
|
s := scanner.new_scanner(text, .skip_comments)
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var p := Parser{
|
2019-12-22 02:34:37 +01:00
|
|
|
|
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-04-04 05:14:40 +02:00
|
|
|
|
global_scope: &ast.Scope{
|
2020-04-08 00:59:28 +02:00
|
|
|
|
start_pos: 0
|
|
|
|
|
parent: 0
|
|
|
|
|
}
|
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-04-04 05:14:40 +02:00
|
|
|
|
pub fn parse_file(path string, table &table.Table, comments_mode scanner.CommentsMode, pref &pref.Preferences, global_scope &ast.Scope) ast.File {
|
2020-02-29 17:51:35 +01:00
|
|
|
|
// println('parse_file("$path")')
|
2020-02-29 17:53:04 +01:00
|
|
|
|
// text := os.read_file(path) or {
|
|
|
|
|
// panic(err)
|
|
|
|
|
// }
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var stmts := []ast.Stmt
|
|
|
|
|
var p := Parser{
|
2020-02-29 17:51:35 +01:00
|
|
|
|
scanner: scanner.new_scanner_file(path, comments_mode)
|
2019-12-26 11:27:35 +01:00
|
|
|
|
table: table
|
2020-01-01 10:15:05 +01:00
|
|
|
|
file_name: path
|
2020-04-10 22:27:51 +02:00
|
|
|
|
file_name_dir: os.dir(path)
|
2020-04-05 04:05:09 +02:00
|
|
|
|
pref: pref
|
2020-02-16 11:48:29 +01:00
|
|
|
|
scope: &ast.Scope{
|
2020-04-08 00:59:28 +02:00
|
|
|
|
start_pos: 0
|
|
|
|
|
parent: 0
|
|
|
|
|
}
|
2020-04-04 05:14:40 +02:00
|
|
|
|
global_scope: global_scope
|
2019-12-26 11:27:35 +01:00
|
|
|
|
}
|
2020-04-05 04:05:09 +02:00
|
|
|
|
// comments_mode: comments_mode
|
2019-12-28 14:11:05 +01:00
|
|
|
|
p.read_first_token()
|
2020-04-10 22:27:51 +02:00
|
|
|
|
for p.tok.kind == .comment {
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var stmt := ast.Stmt{} // TODO sum type << bug
|
2020-04-10 22:27:51 +02:00
|
|
|
|
com := p.comment()
|
|
|
|
|
stmt = com
|
|
|
|
|
stmts << stmt
|
|
|
|
|
}
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var mstmt := ast.Stmt{}
|
2020-04-10 22:27:51 +02:00
|
|
|
|
module_decl := p.module_decl()
|
|
|
|
|
mstmt = module_decl
|
|
|
|
|
stmts << mstmt
|
2020-01-18 23:26:14 +01:00
|
|
|
|
// imports
|
2020-02-29 17:51:35 +01:00
|
|
|
|
/*
|
2020-01-18 23:26:14 +01:00
|
|
|
|
mut imports := []ast.Import
|
|
|
|
|
for p.tok.kind == .key_import {
|
|
|
|
|
imports << p.import_stmt()
|
|
|
|
|
}
|
2020-04-07 15:48:13 +02:00
|
|
|
|
*/
|
2020-01-18 23:26:14 +01:00
|
|
|
|
// TODO: import only mode
|
2020-04-10 22:27:51 +02: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-02-29 17:51:35 +01:00
|
|
|
|
imports: p.ast_imports
|
2020-01-22 21:34:38 +01:00
|
|
|
|
stmts: stmts
|
2020-02-20 11:13:18 +01:00
|
|
|
|
scope: p.scope
|
2020-04-04 05:14:40 +02:00
|
|
|
|
global_scope: p.global_scope
|
2019-12-28 14:11:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 10:53:28 +01:00
|
|
|
|
/*
|
2020-03-16 08:33:42 +01:00
|
|
|
|
struct Queue {
|
|
|
|
|
mut:
|
|
|
|
|
idx int
|
|
|
|
|
mu sync.Mutex
|
|
|
|
|
paths []string
|
|
|
|
|
table &table.Table
|
|
|
|
|
parsed_ast_files []ast.File
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn (q mut Queue) run() {
|
|
|
|
|
q.mu.lock()
|
|
|
|
|
idx := q.idx
|
|
|
|
|
if idx >= q.paths.len {
|
|
|
|
|
q.mu.unlock()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
q.idx++
|
|
|
|
|
q.mu.unlock()
|
|
|
|
|
path := q.paths[idx]
|
|
|
|
|
file := parse_file(path, q.table, .skip_comments)
|
|
|
|
|
q.mu.lock()
|
|
|
|
|
q.parsed_ast_files << file
|
|
|
|
|
q.mu.unlock()
|
|
|
|
|
}
|
2020-03-16 10:53:28 +01:00
|
|
|
|
*/
|
2020-04-04 05:14:40 +02:00
|
|
|
|
pub fn parse_files(paths []string, table &table.Table, pref &pref.Preferences, global_scope &ast.Scope) []ast.File {
|
2020-03-16 08:33:42 +01:00
|
|
|
|
/*
|
|
|
|
|
println('\n\n\nparse_files()')
|
|
|
|
|
println(paths)
|
|
|
|
|
nr_cpus := runtime.nr_cpus()
|
|
|
|
|
println('nr_cpus= $nr_cpus')
|
|
|
|
|
mut q := &Queue{
|
|
|
|
|
paths: paths
|
|
|
|
|
table: table
|
|
|
|
|
}
|
|
|
|
|
for i in 0 .. nr_cpus {
|
|
|
|
|
go q.run()
|
|
|
|
|
}
|
|
|
|
|
time.sleep_ms(100)
|
|
|
|
|
return q.parsed_ast_files
|
2020-04-07 15:48:13 +02:00
|
|
|
|
*/
|
2020-03-16 08:33:42 +01:00
|
|
|
|
// ///////////////
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var files := []ast.File
|
2019-12-30 12:10:46 +01:00
|
|
|
|
for path in paths {
|
2020-03-27 08:46:54 +01:00
|
|
|
|
// println('parse_files $path')
|
2020-04-04 05:14:40 +02:00
|
|
|
|
files << parse_file(path, table, .skip_comments, pref, global_scope)
|
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
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
pub fn (p mut Parser) read_first_token() {
|
2019-12-28 14:11:05 +01:00
|
|
|
|
// 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-04-15 04:15:42 +02:00
|
|
|
|
pub fn (p mut Parser) open_scope() {
|
2020-02-15 13:37:48 +01:00
|
|
|
|
p.scope = &ast.Scope{
|
|
|
|
|
parent: p.scope
|
|
|
|
|
start_pos: p.tok.pos
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
pub fn (p mut Parser) close_scope() {
|
2020-02-15 13:37:48 +01:00
|
|
|
|
p.scope.end_pos = p.tok.pos
|
|
|
|
|
p.scope.parent.children << p.scope
|
|
|
|
|
p.scope = p.scope.parent
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
pub fn (p mut Parser) parse_block() []ast.Stmt {
|
2020-02-15 13:37:48 +01:00
|
|
|
|
p.open_scope()
|
2020-03-18 09:56:19 +01:00
|
|
|
|
// println('parse block')
|
|
|
|
|
stmts := p.parse_block_no_scope()
|
|
|
|
|
p.close_scope()
|
|
|
|
|
// println('nr exprs in block = $exprs.len')
|
|
|
|
|
return stmts
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
pub fn (p mut Parser) parse_block_no_scope() []ast.Stmt {
|
2019-12-31 19:42:16 +01:00
|
|
|
|
p.check(.lcbr)
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var stmts := []ast.Stmt
|
2020-01-06 16:13:12 +01:00
|
|
|
|
if p.tok.kind != .rcbr {
|
2020-04-10 22:27:51 +02:00
|
|
|
|
for {
|
2020-01-06 16:13:12 +01:00
|
|
|
|
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)
|
2019-12-28 14:11:05 +01:00
|
|
|
|
return stmts
|
2019-12-27 13:57:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 02:08:10 +02:00
|
|
|
|
/*
|
|
|
|
|
fn (p mut Parser) next_with_comment() {
|
|
|
|
|
p.tok = p.peek_tok
|
|
|
|
|
p.peek_tok = p.scanner.scan()
|
|
|
|
|
}
|
|
|
|
|
*/
|
2020-04-15 04:15:42 +02: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()
|
2020-04-05 02:08:10 +02:00
|
|
|
|
/*
|
|
|
|
|
if p.tok.kind==.comment {
|
|
|
|
|
p.comments << ast.Comment{text:p.tok.lit, line_nr:p.tok.line_nr}
|
|
|
|
|
p.next()
|
|
|
|
|
}
|
2020-04-07 15:48:13 +02:00
|
|
|
|
*/
|
2019-12-22 02:34:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) check(expected token.Kind) {
|
2020-02-29 17:51:35 +01:00
|
|
|
|
// for p.tok.kind in [.line_comment, .mline_comment] {
|
|
|
|
|
// p.next()
|
|
|
|
|
// }
|
2019-12-28 09:15:32 +01:00
|
|
|
|
if p.tok.kind != expected {
|
2020-04-03 17:38:41 +02:00
|
|
|
|
s := '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()
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
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-04-15 04:15:42 +02: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()
|
|
|
|
|
}
|
2020-04-02 12:53:15 +02:00
|
|
|
|
.key_struct, .key_union {
|
2019-12-31 19:42:16 +01:00
|
|
|
|
return p.struct_decl()
|
|
|
|
|
}
|
2020-04-02 12:53:15 +02:00
|
|
|
|
.key_interface {
|
|
|
|
|
return p.interface_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-04-07 15:48:13 +02:00
|
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
|
}
|
2020-02-03 07:02:54 +01:00
|
|
|
|
.lsbr {
|
2020-03-05 16:13:14 +01:00
|
|
|
|
return p.attribute()
|
2020-02-03 07:02:54 +01:00
|
|
|
|
}
|
2020-03-31 19:59:38 +02:00
|
|
|
|
.key_interface {
|
|
|
|
|
return p.interface_decl()
|
|
|
|
|
}
|
2020-02-29 17:51:35 +01:00
|
|
|
|
.key_import {
|
|
|
|
|
node := p.import_stmt()
|
|
|
|
|
p.ast_imports << node
|
|
|
|
|
return node[0]
|
|
|
|
|
}
|
2020-02-03 07:02:54 +01:00
|
|
|
|
.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-04-05 02:08:10 +02:00
|
|
|
|
.comment {
|
|
|
|
|
return p.comment()
|
2020-02-18 20:20:15 +01:00
|
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
|
else {
|
2020-04-09 12:46:16 +02:00
|
|
|
|
if p.pref.is_script && !p.pref.is_test {
|
2020-04-11 14:06:26 +02:00
|
|
|
|
p.scanner.add_fn_main_and_rescan()
|
|
|
|
|
p.read_first_token()
|
2020-04-09 12:46:16 +02:00
|
|
|
|
return p.top_stmt()
|
2020-04-09 15:05:06 +02:00
|
|
|
|
} else {
|
2020-04-09 12:46:16 +02:00
|
|
|
|
p.error('bad top level statement ' + p.tok.str())
|
|
|
|
|
return ast.Stmt{}
|
|
|
|
|
}
|
2019-12-28 09:43:22 +01:00
|
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 02:08:10 +02:00
|
|
|
|
// TODO [if vfmt]
|
2020-04-15 04:15:42 +02:00
|
|
|
|
pub fn (p mut Parser) check_comment() ast.Comment {
|
2020-04-05 02:08:10 +02:00
|
|
|
|
if p.tok.kind == .comment {
|
|
|
|
|
return p.comment()
|
|
|
|
|
}
|
2020-04-07 15:15:45 +02:00
|
|
|
|
return ast.Comment{}
|
2020-04-05 02:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
pub fn (p mut Parser) comment() ast.Comment {
|
2020-04-05 12:25:33 +02:00
|
|
|
|
pos := p.tok.position()
|
2020-02-29 17:51:35 +01:00
|
|
|
|
text := p.tok.lit
|
|
|
|
|
p.next()
|
2020-04-07 15:48:13 +02:00
|
|
|
|
// p.next_with_comment()
|
2020-04-05 02:08:10 +02:00
|
|
|
|
return ast.Comment{
|
2020-02-29 17:51:35 +01:00
|
|
|
|
text: text
|
2020-04-05 12:25:33 +02:00
|
|
|
|
pos: pos
|
2020-02-29 17:51:35 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
pub fn (p mut Parser) stmt() ast.Stmt {
|
2020-04-14 17:41:14 +02:00
|
|
|
|
p.is_stmt_ident = p.tok.kind == .name
|
2020-01-06 16:13:12 +01:00
|
|
|
|
match p.tok.kind {
|
2020-03-24 15:44:17 +01:00
|
|
|
|
.lcbr {
|
|
|
|
|
stmts := p.parse_block()
|
|
|
|
|
return ast.Block{
|
|
|
|
|
stmts: stmts
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-26 15:51:05 +01:00
|
|
|
|
.key_assert {
|
|
|
|
|
p.next()
|
2020-04-08 04:47:29 +02:00
|
|
|
|
assert_pos := p.tok.position()
|
2020-03-05 12:08:43 +01:00
|
|
|
|
expr := p.expr(0)
|
2020-02-26 15:51:05 +01:00
|
|
|
|
return ast.AssertStmt{
|
|
|
|
|
expr: expr
|
2020-04-08 04:47:29 +02:00
|
|
|
|
pos: assert_pos
|
2020-02-26 15:51:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-15 01:45:27 +02:00
|
|
|
|
.key_mut, .key_static, .key_var {
|
2020-03-10 12:01:37 +01:00
|
|
|
|
return p.assign_stmt()
|
2019-12-28 11:02:06 +01:00
|
|
|
|
}
|
2019-12-30 06:16:59 +01:00
|
|
|
|
.key_for {
|
2020-04-08 19:08:54 +02:00
|
|
|
|
return p.for_stmt()
|
2019-12-30 06:16:59 +01:00
|
|
|
|
}
|
2020-04-05 02:08:10 +02:00
|
|
|
|
.comment {
|
|
|
|
|
return p.comment()
|
2020-02-29 17:51:35 +01:00
|
|
|
|
}
|
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-03-19 07:59:01 +01:00
|
|
|
|
.hash {
|
|
|
|
|
return p.hash()
|
|
|
|
|
}
|
2020-02-11 10:26:46 +01:00
|
|
|
|
.key_defer {
|
|
|
|
|
p.next()
|
|
|
|
|
stmts := p.parse_block()
|
|
|
|
|
return ast.DeferStmt{
|
|
|
|
|
stmts: stmts
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-16 08:33:42 +01:00
|
|
|
|
.key_go {
|
|
|
|
|
p.next()
|
2020-04-03 15:18:17 +02:00
|
|
|
|
expr := p.expr(0)
|
2020-04-07 15:48:13 +02:00
|
|
|
|
// mut call_expr := &ast.CallExpr(0) // TODO
|
2020-04-03 15:18:17 +02:00
|
|
|
|
match expr {
|
|
|
|
|
ast.CallExpr {
|
2020-04-07 15:48:13 +02:00
|
|
|
|
// call_expr = it
|
2020-04-03 15:18:17 +02:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
p.error('expression in `go` must be a function call')
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-16 08:33:42 +01:00
|
|
|
|
return ast.GoStmt{
|
2020-04-03 15:18:17 +02:00
|
|
|
|
call_expr: expr
|
2020-03-16 08:33:42 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
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] {
|
2020-03-10 12:01:37 +01:00
|
|
|
|
return p.assign_stmt()
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else if p.tok.kind == .name && p.peek_tok.kind == .colon {
|
2020-04-05 04:05:09 +02:00
|
|
|
|
// `label:`
|
2020-02-17 14:15:42 +01:00
|
|
|
|
name := p.check_name()
|
|
|
|
|
p.check(.colon)
|
|
|
|
|
return ast.GotoLabel{
|
|
|
|
|
name: name
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-07 16:36:00 +02:00
|
|
|
|
epos := p.tok.position()
|
2020-03-05 12:08:43 +01:00
|
|
|
|
expr := p.expr(0)
|
2019-12-28 14:11:05 +01:00
|
|
|
|
return ast.ExprStmt{
|
|
|
|
|
expr: expr
|
2020-04-07 16:36:00 +02:00
|
|
|
|
pos: epos
|
2019-12-28 14:11:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-18 01:19:23 +01:00
|
|
|
|
// TODO: is it possible to merge with AssignStmt?
|
2020-04-15 04:15:42 +02:00
|
|
|
|
pub fn (p mut Parser) assign_expr(left ast.Expr) ast.AssignExpr {
|
2020-01-06 16:13:12 +01:00
|
|
|
|
op := p.tok.kind
|
|
|
|
|
p.next()
|
2020-04-10 14:11:05 +02:00
|
|
|
|
pos := p.tok.position()
|
2020-03-05 12:08:43 +01:00
|
|
|
|
val := p.expr(0)
|
2020-04-02 23:42:08 +02:00
|
|
|
|
match left {
|
|
|
|
|
ast.IndexExpr {
|
2020-04-07 15:48:13 +02:00
|
|
|
|
// it.mark_as_setter()
|
2020-04-02 23:42:08 +02:00
|
|
|
|
it.is_setter = true
|
|
|
|
|
}
|
2020-04-07 15:48:13 +02:00
|
|
|
|
else {}
|
2020-04-02 23:42:08 +02:00
|
|
|
|
}
|
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
|
2020-04-10 14:11:05 +02:00
|
|
|
|
pos: pos
|
2020-01-06 16:13:12 +01:00
|
|
|
|
}
|
|
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) attribute() ast.Attr {
|
2020-02-03 07:02:54 +01:00
|
|
|
|
p.check(.lsbr)
|
2020-02-18 17:29:47 +01:00
|
|
|
|
if p.tok.kind == .key_if {
|
|
|
|
|
p.next()
|
|
|
|
|
}
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var name := p.check_name()
|
2020-04-12 17:45:04 +02:00
|
|
|
|
if p.tok.kind == .colon {
|
|
|
|
|
p.next()
|
|
|
|
|
if p.tok.kind == .name {
|
|
|
|
|
name += p.check_name()
|
|
|
|
|
} else if p.tok.kind == .string {
|
|
|
|
|
name += p.tok.lit
|
|
|
|
|
p.next()
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-03 07:02:54 +01:00
|
|
|
|
p.check(.rsbr)
|
2020-03-05 16:13:14 +01:00
|
|
|
|
p.attr = name
|
2020-02-03 07:02:54 +01:00
|
|
|
|
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 {
|
2020-03-05 12:08:43 +01:00
|
|
|
|
high = p.expr(0)
|
2020-02-07 07:34:18 +01:00
|
|
|
|
// 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
|
|
|
|
*/
|
2019-12-29 06:50:08 +01:00
|
|
|
|
pub fn (p &Parser) error(s string) {
|
2020-04-10 21:00:54 +02:00
|
|
|
|
p.error_with_pos(s, p.tok.position())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn (p &Parser) warn(s string) {
|
|
|
|
|
p.warn_with_pos(s, p.tok.position())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn (p &Parser) error_with_pos(s string, pos token.Position) {
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var kind := 'error:'
|
2020-04-07 00:44:19 +02:00
|
|
|
|
if p.pref.is_verbose {
|
2020-04-03 17:38:41 +02:00
|
|
|
|
print_backtrace()
|
|
|
|
|
kind = 'parser error:'
|
2020-01-09 14:08:33 +01:00
|
|
|
|
}
|
2020-04-10 21:00:54 +02:00
|
|
|
|
ferror := util.formatted_error(kind, s, p.file_name, pos)
|
2020-04-03 17:38:41 +02:00
|
|
|
|
eprintln(ferror)
|
2020-01-02 08:30:15 +01:00
|
|
|
|
exit(1)
|
2019-12-30 09:38:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-10 21:00:54 +02:00
|
|
|
|
pub fn (p &Parser) warn_with_pos(s string, pos token.Position) {
|
|
|
|
|
ferror := util.formatted_error('warning:', s, p.file_name, pos)
|
2020-04-03 17:38:41 +02:00
|
|
|
|
eprintln(ferror)
|
2019-12-29 08:51:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02: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-03-06 10:52:03 +01:00
|
|
|
|
pos := p.tok.position()
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var 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
|
2020-03-06 10:52:03 +01:00
|
|
|
|
pos: pos
|
2020-02-27 11:12:30 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-28 13:29:04 +01:00
|
|
|
|
if p.expr_mod.len > 0 {
|
|
|
|
|
name = '${p.expr_mod}.$name'
|
|
|
|
|
}
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var 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-04-04 05:14:40 +02:00
|
|
|
|
mod: p.mod
|
2020-03-06 10:52:03 +01:00
|
|
|
|
pos: pos
|
2020-02-06 17:38:02 +01:00
|
|
|
|
}
|
2020-02-28 13:29:04 +01:00
|
|
|
|
return ident
|
2020-02-06 17:38:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) struct_init(short_syntax bool) ast.StructInit {
|
2020-04-01 23:23:20 +02:00
|
|
|
|
typ := if short_syntax { table.void_type } else { p.parse_type() }
|
2020-03-03 14:24:15 +01:00
|
|
|
|
p.expr_mod = ''
|
2020-03-05 13:57:05 +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-04-01 23:23:20 +02:00
|
|
|
|
if !short_syntax {
|
|
|
|
|
p.check(.lcbr)
|
|
|
|
|
}
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var field_names := []string
|
|
|
|
|
var exprs := []ast.Expr
|
|
|
|
|
var i := 0
|
2020-04-08 19:08:54 +02:00
|
|
|
|
is_short_syntax := p.peek_tok.kind != .colon && p.tok.kind != .rcbr // `Vec{a,b,c}
|
2020-02-26 15:51:05 +01:00
|
|
|
|
// p.warn(is_short_syntax.str())
|
2020-02-12 17:39:35 +01:00
|
|
|
|
for p.tok.kind != .rcbr {
|
2020-04-05 12:25:33 +02:00
|
|
|
|
p.check_comment()
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var field_name := ''
|
2020-02-26 15:51:05 +01:00
|
|
|
|
if is_short_syntax {
|
2020-03-05 12:08:43 +01:00
|
|
|
|
expr := p.expr(0)
|
2020-02-26 15:51:05 +01:00
|
|
|
|
exprs << expr
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else {
|
2020-02-26 15:51:05 +01:00
|
|
|
|
field_name = p.check_name()
|
|
|
|
|
field_names << field_name
|
|
|
|
|
}
|
|
|
|
|
if !is_short_syntax {
|
|
|
|
|
p.check(.colon)
|
2020-03-05 12:08:43 +01:00
|
|
|
|
expr := p.expr(0)
|
2020-02-26 15:51:05 +01:00
|
|
|
|
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-04-05 12:25:33 +02:00
|
|
|
|
p.check_comment()
|
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()
|
|
|
|
|
}
|
2020-04-01 23:23:20 +02:00
|
|
|
|
if !short_syntax {
|
|
|
|
|
p.check(.rcbr)
|
|
|
|
|
}
|
2020-02-18 08:58:20 +01:00
|
|
|
|
return node
|
2020-02-12 17:39:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
pub fn (p mut Parser) name_expr() ast.Expr {
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var node := ast.Expr{}
|
2020-02-19 07:16:38 +01:00
|
|
|
|
is_c := p.tok.lit == 'C'
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var 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()
|
2020-03-07 16:23:10 +01:00
|
|
|
|
return ast.MapInit{
|
2020-03-02 12:34:02 +01:00
|
|
|
|
typ: map_type
|
|
|
|
|
}
|
2020-02-27 00:12:37 +01:00
|
|
|
|
}
|
2020-03-24 17:07:27 +01:00
|
|
|
|
// Raw string (`s := r'hello \n ')
|
2020-04-01 17:14:17 +02:00
|
|
|
|
if p.tok.lit in ['r', 'c'] && p.peek_tok.kind == .string {
|
2020-04-01 17:52:50 +02:00
|
|
|
|
// QTODO
|
2020-03-24 17:07:27 +01:00
|
|
|
|
// && p.prev_tok.kind != .str_dollar {
|
|
|
|
|
return p.string_expr()
|
|
|
|
|
}
|
2020-03-01 16:14:52 +01:00
|
|
|
|
known_var := p.scope.known_var(p.tok.lit)
|
2020-04-08 19:47:17 +02:00
|
|
|
|
if p.peek_tok.kind == .dot && !known_var && (is_c || p.known_import(p.tok.lit) || p.mod.all_after('.') ==
|
|
|
|
|
p.tok.lit) {
|
2020-02-29 11:47:47 +01:00
|
|
|
|
if is_c {
|
|
|
|
|
mod = 'C'
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else {
|
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-04-15 01:55:51 +02:00
|
|
|
|
var name := p.tok.lit
|
2020-02-29 11:47:47 +01:00
|
|
|
|
if mod.len > 0 {
|
|
|
|
|
name = '${mod}.$name'
|
|
|
|
|
}
|
2020-03-01 16:14:52 +01:00
|
|
|
|
name_w_mod := p.prepend_mod(name)
|
2020-02-07 21:29:28 +01:00
|
|
|
|
// type cast. TODO: finish
|
2020-02-10 20:33:34 +01:00
|
|
|
|
// if name in table.builtin_type_names {
|
2020-04-08 01:00:58 +02:00
|
|
|
|
if (name in p.table.type_idxs || name_w_mod in p.table.type_idxs) && !(name in ['C.stat',
|
2020-04-07 18:51:39 +02:00
|
|
|
|
'C.sigaction']) {
|
2020-02-11 10:26:46 +01:00
|
|
|
|
// TODO handle C.stat()
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var to_typ := p.parse_type()
|
2020-03-10 23:21:26 +01:00
|
|
|
|
if p.is_amp {
|
|
|
|
|
// Handle `&Foo(0)`
|
|
|
|
|
to_typ = table.type_to_ptr(to_typ)
|
|
|
|
|
}
|
2020-02-10 14:43:17 +01:00
|
|
|
|
p.check(.lpar)
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var expr := ast.Expr{}
|
|
|
|
|
var arg := ast.Expr{}
|
|
|
|
|
var has_arg := false
|
2020-03-05 12:08:43 +01:00
|
|
|
|
expr = p.expr(0)
|
2020-02-10 20:33:34 +01:00
|
|
|
|
// 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)
|
2020-04-07 15:48:13 +02:00
|
|
|
|
arg = p.expr(0) // len
|
2020-03-07 16:23:10 +01:00
|
|
|
|
has_arg = true
|
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-03-07 16:23:10 +01:00
|
|
|
|
arg: arg
|
|
|
|
|
has_arg: has_arg
|
2020-02-10 14:43:17 +01:00
|
|
|
|
}
|
2020-02-27 00:12:37 +01:00
|
|
|
|
p.expr_mod = ''
|
2020-02-18 08:58:20 +01:00
|
|
|
|
return node
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else {
|
2020-04-05 04:05:09 +02:00
|
|
|
|
// fn call
|
2020-02-18 08:58:20 +01:00
|
|
|
|
// println('calling $p.tok.lit')
|
2020-04-07 15:48:13 +02:00
|
|
|
|
x := p.call_expr(is_c, mod) // TODO `node,typ :=` should work
|
2020-02-07 21:29:28 +01:00
|
|
|
|
node = x
|
|
|
|
|
}
|
2020-04-08 19:47:17 +02:00
|
|
|
|
} else if p.peek_tok.kind == .lcbr && (p.tok.lit[0].is_capital() || is_c || (p.builtin_mod &&
|
2020-04-15 16:23:03 +02:00
|
|
|
|
p.tok.lit in table.builtin_type_names)) && !p.inside_match && !p.inside_match_case && !p.inside_if && !p.inside_for {
|
2020-04-08 19:08:54 +02:00
|
|
|
|
// (p.tok.lit.len in [1, 2] || !p.tok.lit[p.tok.lit.len - 1].is_capital()) &&
|
|
|
|
|
// || p.table.known_type(p.tok.lit)) {
|
2020-04-08 19:47:17 +02:00
|
|
|
|
return p.struct_init(false) // short_syntax: false
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else if p.peek_tok.kind == .dot && (p.tok.lit[0].is_capital() && !known_var) {
|
2020-02-27 00:12:37 +01:00
|
|
|
|
// `Color.green`
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var enum_name := p.check_name()
|
2020-02-27 00:12:37 +01:00
|
|
|
|
if mod != '' {
|
|
|
|
|
enum_name = mod + '.' + enum_name
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else {
|
2020-02-27 00:12:37 +01:00
|
|
|
|
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-04-07 15:48:13 +02:00
|
|
|
|
enum_name: enum_name
|
2020-02-25 15:02:34 +01:00
|
|
|
|
val: val
|
2020-02-27 00:12:37 +01:00
|
|
|
|
pos: p.tok.position()
|
2020-03-15 00:46:08 +01:00
|
|
|
|
mod: mod
|
2020-02-25 15:02:34 +01:00
|
|
|
|
}
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else {
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var 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-04-15 04:15:42 +02:00
|
|
|
|
pub fn (p mut Parser) expr(precedence int) ast.Expr {
|
2020-01-22 21:34:38 +01:00
|
|
|
|
// println('\n\nparser.expr()')
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var typ := table.void_type
|
|
|
|
|
var node := ast.Expr{}
|
2020-04-14 17:41:14 +02:00
|
|
|
|
is_stmt_ident := p.is_stmt_ident
|
|
|
|
|
p.is_stmt_ident = false
|
2020-04-07 15:48:13 +02:00
|
|
|
|
// defer {
|
|
|
|
|
// if p.tok.kind == .comment {
|
|
|
|
|
// p.comment()
|
|
|
|
|
// }
|
|
|
|
|
// }
|
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()
|
2020-04-14 17:41:14 +02:00
|
|
|
|
p.is_stmt_ident = is_stmt_ident
|
2019-12-28 09:15:32 +01:00
|
|
|
|
}
|
2020-03-15 00:46:08 +01:00
|
|
|
|
.string {
|
2020-03-05 12:08:43 +01:00
|
|
|
|
node = 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 = p.enum_val()
|
2020-02-10 20:33:34 +01:00
|
|
|
|
}
|
2020-02-04 09:54:15 +01:00
|
|
|
|
.chartoken {
|
|
|
|
|
node = ast.CharLiteral{
|
|
|
|
|
val: p.tok.lit
|
|
|
|
|
}
|
|
|
|
|
p.next()
|
|
|
|
|
}
|
2020-02-05 10:00:11 +01:00
|
|
|
|
.minus, .amp, .mul, .not, .bit_not {
|
2020-04-05 04:05:09 +02:00
|
|
|
|
// -1, -a, !x, &x, ~x
|
2020-02-18 08:58:20 +01:00
|
|
|
|
node = p.prefix_expr()
|
2019-12-30 09:38:12 +01:00
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
p.next()
|
|
|
|
|
}
|
2020-02-05 10:00:11 +01:00
|
|
|
|
.key_match {
|
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-03-05 12:08:43 +01:00
|
|
|
|
node = 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-03-05 12:08:43 +01:00
|
|
|
|
node = 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-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-04-07 15:48:13 +02:00
|
|
|
|
p.next() // sizeof
|
2020-02-04 09:54:15 +01:00
|
|
|
|
p.check(.lpar)
|
2020-04-02 13:44:46 +02:00
|
|
|
|
if p.tok.lit == 'C' {
|
2020-02-19 16:12:39 +01:00
|
|
|
|
p.next()
|
2020-04-02 13:44:46 +02:00
|
|
|
|
p.check(.dot)
|
|
|
|
|
node = ast.SizeOf{
|
|
|
|
|
type_name: p.check_name()
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
sizeof_type := p.parse_type()
|
|
|
|
|
node = ast.SizeOf{
|
|
|
|
|
typ: sizeof_type
|
|
|
|
|
}
|
2020-02-19 16:12:39 +01:00
|
|
|
|
}
|
2020-02-04 09:54:15 +01:00
|
|
|
|
p.check(.rpar)
|
|
|
|
|
}
|
2020-03-19 12:15:39 +01:00
|
|
|
|
.key_typeof {
|
|
|
|
|
p.next()
|
|
|
|
|
p.check(.lpar)
|
|
|
|
|
expr := p.expr(0)
|
|
|
|
|
p.check(.rpar)
|
|
|
|
|
node = ast.TypeOf{
|
|
|
|
|
expr: expr
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-11 00:07:01 +01:00
|
|
|
|
.lcbr {
|
2020-04-05 04:05:09 +02:00
|
|
|
|
// Map `{"age": 20}` or `{ x | foo:bar, a:10 }`
|
2020-02-11 00:07:01 +01:00
|
|
|
|
p.next()
|
2020-03-16 03:19:26 +01:00
|
|
|
|
if p.tok.kind == .string {
|
2020-04-01 23:23:20 +02:00
|
|
|
|
node = p.map_init()
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else {
|
2020-04-11 13:56:55 +02:00
|
|
|
|
// it should be a struct
|
2020-04-01 23:23:20 +02:00
|
|
|
|
if p.peek_tok.kind == .pipe {
|
|
|
|
|
node = p.assoc()
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else if p.peek_tok.kind == .colon || p.tok.kind == .rcbr {
|
|
|
|
|
node = p.struct_init(true) // short_syntax: true
|
2020-04-11 13:56:55 +02:00
|
|
|
|
} else if p.tok.kind == .name {
|
|
|
|
|
p.next()
|
|
|
|
|
lit := if p.tok.lit != '' { p.tok.lit } else { p.tok.kind.str() }
|
|
|
|
|
p.error('unexpected ‘$lit‘, expecting ‘:‘')
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else {
|
2020-04-11 13:56:55 +02:00
|
|
|
|
p.error('unexpected ‘$p.tok.lit‘, expecting struct key')
|
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-04-05 02:08:10 +02:00
|
|
|
|
if p.tok.kind == .comment {
|
|
|
|
|
println(p.tok.lit)
|
|
|
|
|
}
|
|
|
|
|
p.error('expr(): bad token `$p.tok.kind.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)
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else if p.tok.kind == .dot {
|
2020-03-06 10:52:03 +01:00
|
|
|
|
node = p.dot_expr(node)
|
2020-04-14 17:41:14 +02:00
|
|
|
|
p.is_stmt_ident = is_stmt_ident
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else if p.tok.kind == .lsbr {
|
2020-02-20 15:42:56 +01:00
|
|
|
|
node = p.index_expr(node)
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else if p.tok.kind == .key_as {
|
2020-03-18 14:50:21 +01:00
|
|
|
|
pos := p.tok.position()
|
2020-02-07 09:19:45 +01:00
|
|
|
|
p.next()
|
|
|
|
|
typ = p.parse_type()
|
2020-03-04 02:50:32 +01:00
|
|
|
|
node = ast.AsCast{
|
2020-03-06 22:12:15 +01:00
|
|
|
|
expr: node
|
2020-03-02 19:00:33 +01:00
|
|
|
|
typ: typ
|
2020-03-18 14:50:21 +01:00
|
|
|
|
pos: pos
|
2020-03-02 19:00:33 +01:00
|
|
|
|
}
|
2020-04-14 17:41:14 +02:00
|
|
|
|
} else if p.tok.kind == .left_shift && p.is_stmt_ident {
|
|
|
|
|
// arr << elem
|
2020-03-03 16:05:16 +01:00
|
|
|
|
tok := p.tok
|
2020-04-10 22:27:51 +02:00
|
|
|
|
pos := tok.position()
|
2020-03-03 16:05:16 +01:00
|
|
|
|
p.next()
|
2020-03-05 12:08:43 +01:00
|
|
|
|
right := p.expr(precedence - 1)
|
2020-03-03 16:05:16 +01:00
|
|
|
|
node = ast.InfixExpr{
|
|
|
|
|
left: node
|
|
|
|
|
right: right
|
|
|
|
|
op: tok.kind
|
2020-04-10 22:27:51 +02:00
|
|
|
|
pos: pos
|
2020-03-03 16:05:16 +01:00
|
|
|
|
}
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else if p.tok.kind.is_infix() {
|
2020-03-05 12:08:43 +01:00
|
|
|
|
node = p.infix_expr(node)
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else if p.tok.kind in [.inc, .dec] {
|
2020-04-05 04:05:09 +02:00
|
|
|
|
// Postfix
|
2020-01-06 16:13:12 +01:00
|
|
|
|
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-04-01 13:18:52 +02:00
|
|
|
|
// return node // TODO bring back, only allow ++/-- in exprs in translated code
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else {
|
2020-03-05 12:08:43 +01:00
|
|
|
|
return node
|
2020-01-06 16:13:12 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-05 12:08:43 +01:00
|
|
|
|
return node
|
2020-01-06 16:13:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) prefix_expr() ast.PrefixExpr {
|
2020-03-28 14:38:16 +01:00
|
|
|
|
pos := p.tok.position()
|
2020-01-06 16:13:12 +01:00
|
|
|
|
op := p.tok.kind
|
2020-03-10 23:21:26 +01:00
|
|
|
|
if op == .amp {
|
|
|
|
|
p.is_amp = true
|
|
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
|
p.next()
|
2020-04-08 13:53:11 +02:00
|
|
|
|
right := p.expr(token.Precedence.prefix)
|
2020-03-10 23:21:26 +01:00
|
|
|
|
p.is_amp = false
|
2020-02-18 08:58:20 +01:00
|
|
|
|
return ast.PrefixExpr{
|
2020-01-06 16:13:12 +01:00
|
|
|
|
op: op
|
|
|
|
|
right: right
|
2020-03-28 14:38:16 +01:00
|
|
|
|
pos: pos
|
2020-01-06 16:13:12 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02: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-04-07 15:48:13 +02:00
|
|
|
|
p.next() // [
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var has_low := true
|
2020-02-10 23:19:50 +01:00
|
|
|
|
if p.tok.kind == .dotdot {
|
2020-03-06 22:24:39 +01:00
|
|
|
|
has_low = false
|
2020-02-10 23:19:50 +01:00
|
|
|
|
// [..end]
|
|
|
|
|
p.next()
|
2020-03-05 12:08:43 +01:00
|
|
|
|
high := p.expr(0)
|
2020-02-10 23:19:50 +01:00
|
|
|
|
p.check(.rsbr)
|
|
|
|
|
return ast.IndexExpr{
|
|
|
|
|
left: left
|
|
|
|
|
pos: p.tok.position()
|
|
|
|
|
index: ast.RangeExpr{
|
2020-04-08 00:59:28 +02:00
|
|
|
|
low: ast.Expr{}
|
|
|
|
|
high: high
|
|
|
|
|
has_high: true
|
|
|
|
|
}
|
2020-02-15 13:37:48 +01:00
|
|
|
|
}
|
2020-02-02 14:31:54 +01:00
|
|
|
|
}
|
2020-04-07 15:48:13 +02:00
|
|
|
|
expr := p.expr(0) // `[expr]` or `[expr..]`
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var has_high := false
|
2020-02-10 23:19:50 +01:00
|
|
|
|
if p.tok.kind == .dotdot {
|
|
|
|
|
// [start..end] or [start..]
|
|
|
|
|
p.check(.dotdot)
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var high := ast.Expr{}
|
2020-02-10 23:19:50 +01:00
|
|
|
|
if p.tok.kind != .rsbr {
|
2020-03-06 22:24:39 +01:00
|
|
|
|
has_high = true
|
2020-03-05 12:08:43 +01:00
|
|
|
|
high = p.expr(0)
|
2020-02-10 23:19:50 +01:00
|
|
|
|
}
|
|
|
|
|
p.check(.rsbr)
|
|
|
|
|
return ast.IndexExpr{
|
|
|
|
|
left: left
|
|
|
|
|
pos: p.tok.position()
|
|
|
|
|
index: ast.RangeExpr{
|
2020-04-08 00:59:28 +02:00
|
|
|
|
low: expr
|
|
|
|
|
high: high
|
|
|
|
|
has_high: has_high
|
|
|
|
|
has_low: has_low
|
|
|
|
|
}
|
2020-02-15 13:37:48 +01:00
|
|
|
|
}
|
2020-02-11 12:59:40 +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-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) filter() {
|
2020-04-04 05:14:40 +02:00
|
|
|
|
p.scope.register('it', ast.Var{
|
2020-02-15 13:37:48 +01:00
|
|
|
|
name: 'it'
|
|
|
|
|
})
|
2020-02-10 14:42:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) dot_expr(left ast.Expr) ast.Expr {
|
2020-01-06 16:13:12 +01:00
|
|
|
|
p.next()
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var name_pos := p.tok.position()
|
2020-01-06 16:13:12 +01:00
|
|
|
|
field_name := p.check_name()
|
2020-03-25 10:09:50 +01:00
|
|
|
|
is_filter := field_name in ['filter', 'map']
|
|
|
|
|
if is_filter {
|
2020-02-15 13:37:48 +01:00
|
|
|
|
p.open_scope()
|
2020-04-12 14:19:07 +02:00
|
|
|
|
name_pos = p.tok.position()
|
2020-03-06 10:52:03 +01:00
|
|
|
|
p.filter()
|
|
|
|
|
// wrong tok position when using defer
|
|
|
|
|
// defer {
|
2020-03-06 14:14:33 +01:00
|
|
|
|
// p.close_scope()
|
2020-03-06 10:52:03 +01:00
|
|
|
|
// }
|
2020-02-10 14:42:57 +01:00
|
|
|
|
}
|
2020-03-18 09:56:19 +01:00
|
|
|
|
// Method call
|
2020-01-07 01:08:24 +01:00
|
|
|
|
if p.tok.kind == .lpar {
|
|
|
|
|
p.next()
|
2020-03-14 11:11:43 +01:00
|
|
|
|
args := p.call_args()
|
2020-04-10 14:53:06 +02:00
|
|
|
|
p.check(.rpar)
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var or_stmts := []ast.Stmt
|
|
|
|
|
var is_or_block_used := false
|
2020-02-04 17:44:39 +01:00
|
|
|
|
if p.tok.kind == .key_orelse {
|
|
|
|
|
p.next()
|
2020-03-18 09:56:19 +01:00
|
|
|
|
p.open_scope()
|
2020-04-04 20:47:57 +02:00
|
|
|
|
p.scope.register('errcode', ast.Var{
|
|
|
|
|
name: 'errcode'
|
|
|
|
|
typ: table.int_type
|
|
|
|
|
})
|
2020-04-04 05:14:40 +02:00
|
|
|
|
p.scope.register('err', ast.Var{
|
2020-03-18 09:56:19 +01:00
|
|
|
|
name: 'err'
|
2020-03-18 10:00:33 +01:00
|
|
|
|
typ: table.string_type
|
2020-03-18 09:56:19 +01:00
|
|
|
|
})
|
2020-04-07 16:36:00 +02:00
|
|
|
|
is_or_block_used = true
|
2020-03-18 09:56:19 +01:00
|
|
|
|
or_stmts = p.parse_block_no_scope()
|
|
|
|
|
p.close_scope()
|
2020-02-04 17:44:39 +01:00
|
|
|
|
}
|
2020-04-12 14:19:07 +02:00
|
|
|
|
end_pos := p.tok.position()
|
|
|
|
|
pos := token.Position{
|
|
|
|
|
line_nr: name_pos.line_nr
|
|
|
|
|
pos: name_pos.pos
|
|
|
|
|
len: end_pos.pos - name_pos.pos
|
|
|
|
|
}
|
2020-03-30 12:39:20 +02:00
|
|
|
|
mcall_expr := ast.CallExpr{
|
|
|
|
|
left: left
|
2020-01-07 01:08:24 +01:00
|
|
|
|
name: field_name
|
|
|
|
|
args: args
|
2020-03-06 10:52:03 +01:00
|
|
|
|
pos: pos
|
2020-03-30 12:39:20 +02:00
|
|
|
|
is_method: true
|
2020-02-28 15:36:41 +01:00
|
|
|
|
or_block: ast.OrExpr{
|
2020-04-07 16:36:00 +02:00
|
|
|
|
stmts: or_stmts
|
|
|
|
|
is_used: is_or_block_used
|
|
|
|
|
}
|
2020-01-07 01:08:24 +01:00
|
|
|
|
}
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var node := ast.Expr{}
|
2020-01-18 23:26:14 +01:00
|
|
|
|
node = mcall_expr
|
2020-03-25 10:09:50 +01:00
|
|
|
|
if is_filter {
|
2020-03-06 10:52:03 +01:00
|
|
|
|
p.close_scope()
|
|
|
|
|
}
|
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-04-12 14:19:07 +02:00
|
|
|
|
pos: name_pos
|
2019-12-22 02:34:37 +01:00
|
|
|
|
}
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var node := ast.Expr{}
|
2020-01-18 23:26:14 +01:00
|
|
|
|
node = sel_expr
|
2020-03-25 10:09:50 +01:00
|
|
|
|
if is_filter {
|
2020-03-06 10:52:03 +01:00
|
|
|
|
p.close_scope()
|
|
|
|
|
}
|
2020-02-15 13:37:48 +01:00
|
|
|
|
return node
|
2019-12-24 18:54:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) infix_expr(left ast.Expr) ast.Expr {
|
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-04-15 01:55:51 +02:00
|
|
|
|
var right := ast.Expr{}
|
2020-03-05 12:08:43 +01:00
|
|
|
|
right = p.expr(precedence)
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var 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-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-03-05 12:08:43 +01:00
|
|
|
|
return expr
|
2020-01-06 16:13:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 20:33:34 +01:00
|
|
|
|
// `.green`
|
2020-02-20 15:42:56 +01:00
|
|
|
|
// `pref.BuildMode.default_mode`
|
2020-04-15 04:15:42 +02: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
|
|
|
|
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-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) for_stmt() ast.Stmt {
|
2019-12-30 06:16:59 +01:00
|
|
|
|
p.check(.key_for)
|
2020-03-18 14:50:21 +01:00
|
|
|
|
pos := p.tok.position()
|
2020-02-15 13:37:48 +01:00
|
|
|
|
p.open_scope()
|
2020-04-08 19:08:54 +02:00
|
|
|
|
p.inside_for = true
|
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-04-08 19:08:54 +02:00
|
|
|
|
p.inside_for = false
|
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
|
|
|
|
}
|
2020-04-15 01:45:27 +02:00
|
|
|
|
} else if p.tok.kind in [.key_mut, .key_var] {
|
2020-01-07 00:14:19 +01:00
|
|
|
|
p.error('`mut` is not required in for loops')
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else if p.peek_tok.kind in [.decl_assign, .assign, .semicolon] || p.tok.kind == .semicolon {
|
|
|
|
|
// `for i := 0; i < 10; i++ {`
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var init := ast.Stmt{}
|
|
|
|
|
var cond := p.new_true_expr()
|
2020-03-06 14:14:33 +01:00
|
|
|
|
// mut inc := ast.Stmt{}
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var inc := ast.Expr{}
|
|
|
|
|
var has_init := false
|
|
|
|
|
var has_cond := false
|
|
|
|
|
var has_inc := false
|
2020-02-28 13:29:04 +01:00
|
|
|
|
if p.peek_tok.kind in [.assign, .decl_assign] {
|
2020-03-10 12:01:37 +01:00
|
|
|
|
init = p.assign_stmt()
|
2020-03-11 02:44:30 +01:00
|
|
|
|
has_init = true
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else if p.tok.kind != .semicolon {
|
2020-02-20 20:30:34 +01:00
|
|
|
|
}
|
2020-02-19 16:12:39 +01:00
|
|
|
|
// allow `for ;; i++ {`
|
|
|
|
|
// Allow `for i = 0; i < ...`
|
2020-01-07 00:14:19 +01:00
|
|
|
|
p.check(.semicolon)
|
|
|
|
|
if p.tok.kind != .semicolon {
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var typ := table.void_type
|
2020-03-05 12:08:43 +01:00
|
|
|
|
cond = p.expr(0)
|
2020-04-10 09:00:14 +02:00
|
|
|
|
has_cond = true
|
2020-01-07 00:14:19 +01:00
|
|
|
|
}
|
|
|
|
|
p.check(.semicolon)
|
|
|
|
|
if p.tok.kind != .lcbr {
|
2020-03-06 14:14:33 +01:00
|
|
|
|
// inc = p.stmt()
|
2020-03-05 12:08:43 +01:00
|
|
|
|
inc = p.expr(0)
|
2020-04-10 09:00:14 +02:00
|
|
|
|
has_inc = true
|
2020-01-07 00:14:19 +01:00
|
|
|
|
}
|
2020-04-08 19:08:54 +02:00
|
|
|
|
p.inside_for = false
|
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.ForCStmt{
|
|
|
|
|
stmts: stmts
|
2020-03-11 02:44:30 +01:00
|
|
|
|
has_init: has_init
|
2020-04-10 09:00:14 +02:00
|
|
|
|
has_cond: has_cond
|
|
|
|
|
has_inc: has_inc
|
2020-01-07 00:14:19 +01:00
|
|
|
|
init: init
|
|
|
|
|
cond: cond
|
|
|
|
|
inc: inc
|
2020-03-18 14:50:21 +01:00
|
|
|
|
pos: pos
|
2020-01-07 00:14:19 +01:00
|
|
|
|
}
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else if p.peek_tok.kind in [.key_in, .comma] {
|
2020-04-05 04:05:09 +02:00
|
|
|
|
// `for i in vals`, `for i in start .. end`
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var key_var_name := ''
|
|
|
|
|
var val_var_name := p.check_name()
|
2020-02-04 17:44:39 +01:00
|
|
|
|
if p.tok.kind == .comma {
|
|
|
|
|
p.check(.comma)
|
2020-03-18 13:18:18 +01:00
|
|
|
|
key_var_name = val_var_name
|
|
|
|
|
val_var_name = p.check_name()
|
2020-04-04 05:14:40 +02:00
|
|
|
|
p.scope.register(key_var_name, ast.Var{
|
2020-03-18 13:18:18 +01:00
|
|
|
|
name: key_var_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 20:33:34 +01:00
|
|
|
|
// arr_expr
|
2020-03-05 12:08:43 +01:00
|
|
|
|
cond := p.expr(0)
|
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
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var high_expr := ast.Expr{}
|
|
|
|
|
var 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-03-05 12:08:43 +01:00
|
|
|
|
high_expr = p.expr(0)
|
2020-04-04 05:14:40 +02:00
|
|
|
|
p.scope.register(val_var_name, ast.Var{
|
2020-03-18 13:18:18 +01:00
|
|
|
|
name: val_var_name
|
|
|
|
|
typ: table.int_type
|
|
|
|
|
})
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else {
|
2020-03-18 13:18:18 +01:00
|
|
|
|
// this type will be set in checker
|
2020-04-04 05:14:40 +02:00
|
|
|
|
p.scope.register(val_var_name, ast.Var{
|
2020-03-18 13:18:18 +01:00
|
|
|
|
name: val_var_name
|
|
|
|
|
})
|
2020-02-04 08:29:50 +01:00
|
|
|
|
}
|
2020-04-08 19:08:54 +02:00
|
|
|
|
p.inside_for = false
|
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-22 16:59:50 +01:00
|
|
|
|
cond: cond
|
2020-03-18 13:18:18 +01:00
|
|
|
|
key_var: key_var_name
|
|
|
|
|
val_var: val_var_name
|
2020-02-28 15:36:41 +01:00
|
|
|
|
high: high_expr
|
|
|
|
|
is_range: is_range
|
2020-03-18 14:50:21 +01:00
|
|
|
|
pos: pos
|
2020-01-01 22:34:46 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// `for cond {`
|
2020-03-05 12:08:43 +01:00
|
|
|
|
cond := p.expr(0)
|
2020-04-08 19:08:54 +02:00
|
|
|
|
p.inside_for = false
|
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-03-18 14:50:21 +01:00
|
|
|
|
pos: pos
|
2019-12-30 06:16:59 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) if_expr() ast.IfExpr {
|
2020-02-19 11:31:33 +01:00
|
|
|
|
pos := p.tok.position()
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var branches := []ast.IfBranch
|
|
|
|
|
var has_else := false
|
2020-03-20 14:39:56 +01:00
|
|
|
|
for p.tok.kind in [.key_if, .key_else] {
|
2020-04-08 19:08:54 +02:00
|
|
|
|
p.inside_if = true
|
2020-03-20 14:39:56 +01:00
|
|
|
|
branch_pos := p.tok.position()
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var comment := ast.Comment{}
|
2020-02-04 17:44:39 +01:00
|
|
|
|
if p.tok.kind == .key_if {
|
2020-03-20 14:39:56 +01:00
|
|
|
|
p.check(.key_if)
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else {
|
|
|
|
|
// if p.tok.kind == .comment {
|
|
|
|
|
// p.error('place comments inside {}')
|
|
|
|
|
// }
|
|
|
|
|
// comment = p.check_comment()
|
2020-03-20 14:39:56 +01:00
|
|
|
|
p.check(.key_else)
|
|
|
|
|
if p.tok.kind == .key_if {
|
|
|
|
|
p.check(.key_if)
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else {
|
2020-03-20 14:39:56 +01:00
|
|
|
|
has_else = true
|
2020-04-08 19:08:54 +02:00
|
|
|
|
p.inside_if = false
|
2020-03-20 14:39:56 +01:00
|
|
|
|
branches << ast.IfBranch{
|
|
|
|
|
stmts: p.parse_block()
|
|
|
|
|
pos: branch_pos
|
2020-04-07 15:15:45 +02:00
|
|
|
|
comment: comment
|
2020-03-20 14:39:56 +01:00
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
2020-02-04 17:44:39 +01:00
|
|
|
|
}
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var cond := ast.Expr{}
|
|
|
|
|
var is_or := false
|
2020-03-20 14:39:56 +01:00
|
|
|
|
// `if x := opt() {`
|
|
|
|
|
if p.peek_tok.kind == .decl_assign {
|
|
|
|
|
is_or = true
|
|
|
|
|
p.open_scope()
|
|
|
|
|
var_name := p.check_name()
|
|
|
|
|
p.check(.decl_assign)
|
|
|
|
|
expr := p.expr(0)
|
2020-04-04 05:14:40 +02:00
|
|
|
|
p.scope.register(var_name, ast.Var{
|
2020-03-20 14:39:56 +01:00
|
|
|
|
name: var_name
|
|
|
|
|
expr: expr
|
|
|
|
|
})
|
|
|
|
|
cond = ast.IfGuardExpr{
|
|
|
|
|
var_name: var_name
|
|
|
|
|
expr: expr
|
2020-02-05 10:00:11 +01:00
|
|
|
|
}
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else {
|
2020-03-20 14:39:56 +01:00
|
|
|
|
cond = p.expr(0)
|
|
|
|
|
}
|
|
|
|
|
p.inside_if = false
|
|
|
|
|
stmts := p.parse_block()
|
|
|
|
|
if is_or {
|
|
|
|
|
p.close_scope()
|
|
|
|
|
}
|
|
|
|
|
branches << ast.IfBranch{
|
|
|
|
|
cond: cond
|
|
|
|
|
stmts: stmts
|
|
|
|
|
pos: branch_pos
|
2020-04-07 15:48:13 +02:00
|
|
|
|
comment: ast.Comment{}
|
2020-03-20 14:39:56 +01:00
|
|
|
|
}
|
|
|
|
|
if p.tok.kind != .key_else {
|
|
|
|
|
break
|
|
|
|
|
}
|
2020-01-02 20:09:15 +01:00
|
|
|
|
}
|
2020-03-20 14:39:56 +01:00
|
|
|
|
return ast.IfExpr{
|
|
|
|
|
branches: branches
|
2020-02-19 11:31:33 +01:00
|
|
|
|
pos: pos
|
2020-02-22 17:07:03 +01:00
|
|
|
|
has_else: has_else
|
2019-12-29 08:51:55 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) string_expr() ast.Expr {
|
2020-03-24 17:07:27 +01:00
|
|
|
|
is_raw := p.tok.kind == .name && p.tok.lit == 'r'
|
|
|
|
|
is_cstr := p.tok.kind == .name && p.tok.lit == 'c'
|
|
|
|
|
if is_raw || is_cstr {
|
|
|
|
|
p.next()
|
|
|
|
|
}
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var node := ast.Expr{}
|
2020-03-21 07:01:06 +01:00
|
|
|
|
val := p.tok.lit
|
2020-04-10 14:40:52 +02:00
|
|
|
|
pos := p.tok.position()
|
2020-01-07 01:08:24 +01:00
|
|
|
|
if p.peek_tok.kind != .str_dollar {
|
|
|
|
|
p.next()
|
2020-04-10 10:59:07 +02:00
|
|
|
|
node = ast.StringLiteral{
|
|
|
|
|
val: val
|
|
|
|
|
is_raw: is_raw
|
|
|
|
|
is_c: is_cstr
|
|
|
|
|
pos: pos
|
|
|
|
|
}
|
2020-03-05 12:08:43 +01:00
|
|
|
|
return node
|
2020-01-07 01:08:24 +01:00
|
|
|
|
}
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var exprs := []ast.Expr
|
|
|
|
|
var vals := []string
|
|
|
|
|
var efmts := []string
|
2020-01-07 01:08:24 +01:00
|
|
|
|
// Handle $ interpolation
|
2020-03-16 03:19:26 +01:00
|
|
|
|
for p.tok.kind == .string {
|
2020-03-21 07:01:06 +01:00
|
|
|
|
vals << p.tok.lit
|
2020-01-07 01:08:24 +01:00
|
|
|
|
p.next()
|
|
|
|
|
if p.tok.kind != .str_dollar {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
p.check(.str_dollar)
|
2020-03-21 07:01:06 +01:00
|
|
|
|
exprs << p.expr(0)
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var efmt := []string
|
2020-02-10 20:33:34 +01:00
|
|
|
|
if p.tok.kind == .colon {
|
2020-03-24 22:18:58 +01:00
|
|
|
|
efmt << ':'
|
2020-02-04 09:54:15 +01:00
|
|
|
|
p.next()
|
2020-04-11 17:25:39 +02:00
|
|
|
|
// ${num:-2d}
|
|
|
|
|
if p.tok.kind == .minus {
|
|
|
|
|
efmt << '-'
|
|
|
|
|
p.next()
|
|
|
|
|
}
|
|
|
|
|
// ${num:2d}
|
|
|
|
|
if p.tok.kind == .number {
|
|
|
|
|
efmt << p.tok.lit
|
|
|
|
|
p.next()
|
|
|
|
|
}
|
2020-02-11 10:26:46 +01:00
|
|
|
|
if p.tok.lit.len == 1 {
|
2020-03-24 22:18:58 +01:00
|
|
|
|
efmt << p.tok.lit
|
2020-02-11 10:26:46 +01:00
|
|
|
|
p.next()
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-24 22:18:58 +01:00
|
|
|
|
efmts << efmt.join('')
|
2020-01-07 01:08:24 +01:00
|
|
|
|
}
|
2020-03-21 07:01:06 +01:00
|
|
|
|
node = ast.StringInterLiteral{
|
|
|
|
|
vals: vals
|
|
|
|
|
exprs: exprs
|
2020-03-24 22:18:58 +01:00
|
|
|
|
expr_fmts: efmts
|
2020-04-10 10:59:07 +02:00
|
|
|
|
pos: pos
|
2020-03-21 07:01:06 +01:00
|
|
|
|
}
|
2020-03-05 12:08:43 +01:00
|
|
|
|
return node
|
2019-12-28 09:15:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) array_init() ast.ArrayInit {
|
2020-04-10 00:09:34 +02:00
|
|
|
|
first_pos := p.tok.position()
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var last_pos := token.Position{}
|
2019-12-30 09:38:12 +01:00
|
|
|
|
p.check(.lsbr)
|
2020-02-12 17:39:35 +01:00
|
|
|
|
// p.warn('array_init() exp=$p.expected_type')
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var array_type := table.void_type
|
|
|
|
|
var elem_type := table.void_type
|
|
|
|
|
var exprs := []ast.Expr
|
|
|
|
|
var is_fixed := false
|
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-03-24 16:24:23 +01:00
|
|
|
|
if p.tok.kind in [.name, .amp] && p.tok.line_nr == line_nr {
|
2020-03-04 02:50:32 +01:00
|
|
|
|
elem_type = p.parse_type()
|
2020-02-19 14:34:44 +01:00
|
|
|
|
// this is set here becasue its a known type, others could be the
|
|
|
|
|
// result of expr so we do those in checker
|
2020-03-04 02:50:32 +01:00
|
|
|
|
idx := p.table.find_or_register_array(elem_type, 1)
|
2020-02-19 14:34:44 +01:00
|
|
|
|
array_type = table.new_type(idx)
|
2020-02-10 14:43:17 +01:00
|
|
|
|
}
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else {
|
2020-04-10 21:00:54 +02:00
|
|
|
|
// [1,2,3] or [const]byte
|
2020-04-08 00:59:28 +02:00
|
|
|
|
for i := 0; p.tok.kind != .rsbr; i++ {
|
2020-03-05 12:08:43 +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-04-07 15:48:13 +02:00
|
|
|
|
// p.check_comment()
|
2020-02-10 14:43:17 +01:00
|
|
|
|
}
|
2020-03-04 02:50:32 +01:00
|
|
|
|
line_nr := p.tok.line_nr
|
2020-04-14 17:02:55 +02:00
|
|
|
|
$if tinyc {
|
|
|
|
|
// NB: do not remove the next line without testing
|
|
|
|
|
// v selfcompilation with tcc first
|
|
|
|
|
tcc_stack_bug := 12345
|
|
|
|
|
}
|
2020-04-13 18:08:02 +02:00
|
|
|
|
last_pos = p.tok.position()
|
2020-02-10 14:43:17 +01:00
|
|
|
|
p.check(.rsbr)
|
2020-03-04 02:50:32 +01:00
|
|
|
|
// [100]byte
|
2020-03-24 16:24:23 +01:00
|
|
|
|
if exprs.len == 1 && p.tok.kind in [.name, .amp] && p.tok.line_nr == line_nr {
|
2020-03-04 02:50:32 +01:00
|
|
|
|
elem_type = p.parse_type()
|
2020-04-10 21:00:54 +02:00
|
|
|
|
is_fixed = true
|
2019-12-30 09:38:12 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-19 16:12:39 +01:00
|
|
|
|
// !
|
|
|
|
|
if p.tok.kind == .not {
|
2020-04-13 18:08:02 +02:00
|
|
|
|
last_pos = p.tok.position()
|
2020-02-19 16:12:39 +01:00
|
|
|
|
p.next()
|
|
|
|
|
}
|
|
|
|
|
if p.tok.kind == .not {
|
2020-04-13 18:08:02 +02:00
|
|
|
|
last_pos = p.tok.position()
|
2020-02-19 16:12:39 +01:00
|
|
|
|
p.next()
|
|
|
|
|
}
|
2020-04-14 18:09:59 +02:00
|
|
|
|
if p.tok.kind == .lcbr && exprs.len == 0 {
|
|
|
|
|
// `[]int{ len: 10, cap: 100}` syntax
|
|
|
|
|
p.next()
|
|
|
|
|
for p.tok.kind != .rcbr {
|
|
|
|
|
key := p.check_name()
|
|
|
|
|
p.check(.colon)
|
|
|
|
|
if !(key in ['len', 'cap', 'init']) {
|
|
|
|
|
p.error('wrong field `$key`, expecting `len`, `cap`, or `init`')
|
|
|
|
|
}
|
|
|
|
|
p.expr(0)
|
|
|
|
|
if p.tok.kind != .rcbr {
|
|
|
|
|
p.check(.comma)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
p.check(.rcbr)
|
|
|
|
|
}
|
2020-04-10 00:09:34 +02:00
|
|
|
|
pos := token.Position{
|
2020-04-10 22:27:51 +02:00
|
|
|
|
line_nr: first_pos.line_nr
|
2020-04-10 00:09:34 +02:00
|
|
|
|
pos: first_pos.pos
|
2020-04-13 18:08:02 +02:00
|
|
|
|
len: last_pos.pos - first_pos.pos + last_pos.len
|
2020-04-10 00:09:34 +02:00
|
|
|
|
}
|
2020-02-19 14:34:44 +01:00
|
|
|
|
return ast.ArrayInit{
|
2020-04-10 21:00:54 +02:00
|
|
|
|
is_fixed: is_fixed
|
|
|
|
|
mod: p.mod
|
2020-03-04 02:50:32 +01:00
|
|
|
|
elem_type: elem_type
|
2020-02-19 14:34:44 +01:00
|
|
|
|
typ: array_type
|
2019-12-30 09:38:12 +01:00
|
|
|
|
exprs: exprs
|
2020-04-10 00:09:34 +02:00
|
|
|
|
pos: pos
|
2019-12-30 09:38:12 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) map_init() ast.MapInit {
|
2020-04-01 23:23:20 +02:00
|
|
|
|
pos := p.tok.position()
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var keys := []ast.Expr
|
|
|
|
|
var vals := []ast.Expr
|
2020-04-01 23:23:20 +02:00
|
|
|
|
for p.tok.kind != .rcbr && p.tok.kind != .eof {
|
|
|
|
|
// p.check(.str)
|
|
|
|
|
key := p.expr(0)
|
|
|
|
|
keys << key
|
|
|
|
|
p.check(.colon)
|
|
|
|
|
val := p.expr(0)
|
|
|
|
|
vals << val
|
|
|
|
|
if p.tok.kind == .comma {
|
|
|
|
|
p.next()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ast.MapInit{
|
|
|
|
|
keys: keys
|
|
|
|
|
vals: vals
|
|
|
|
|
pos: pos
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) parse_number_literal() ast.Expr {
|
2019-12-28 09:15:32 +01:00
|
|
|
|
lit := p.tok.lit
|
2020-04-10 00:09:34 +02:00
|
|
|
|
pos := p.tok.position()
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var node := ast.Expr{}
|
2020-04-08 01:00:58 +02:00
|
|
|
|
if lit.index_any('.eE') >= 0 {
|
2019-12-28 09:15:32 +01:00
|
|
|
|
node = ast.FloatLiteral{
|
|
|
|
|
val: lit
|
|
|
|
|
}
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else {
|
2019-12-28 09:15:32 +01:00
|
|
|
|
node = ast.IntegerLiteral{
|
2020-03-17 02:49:15 +01:00
|
|
|
|
val: lit
|
2020-04-10 00:09:34 +02:00
|
|
|
|
pos: pos
|
2019-12-28 09:15:32 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
p.next()
|
2020-03-05 12:08:43 +01:00
|
|
|
|
return node
|
2019-12-28 09:15:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) module_decl() ast.Module {
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var name := 'main'
|
2020-04-10 22:32:52 +02:00
|
|
|
|
is_skipped := p.tok.kind != .key_module
|
|
|
|
|
if !is_skipped {
|
2020-04-10 22:27:51 +02:00
|
|
|
|
p.check(.key_module)
|
|
|
|
|
name = p.check_name()
|
|
|
|
|
}
|
|
|
|
|
full_mod := p.table.qualify_module(name, p.file_name)
|
2020-02-19 03:08:10 +01:00
|
|
|
|
p.mod = full_mod
|
2020-04-10 22:27:51 +02:00
|
|
|
|
p.builtin_mod = p.mod == 'builtin'
|
2020-01-18 23:26:14 +01:00
|
|
|
|
return ast.Module{
|
2020-02-19 03:08:10 +01:00
|
|
|
|
name: full_mod
|
2020-04-10 22:32:52 +02:00
|
|
|
|
is_skipped: is_skipped
|
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-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) parse_import() ast.Import {
|
2020-03-11 18:52:51 +01:00
|
|
|
|
pos := p.tok.position()
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var mod_name := p.check_name()
|
|
|
|
|
var 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
|
2020-03-11 18:52:51 +01:00
|
|
|
|
pos: pos
|
2020-01-18 23:26:14 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) import_stmt() []ast.Import {
|
2020-01-18 23:26:14 +01:00
|
|
|
|
p.check(.key_import)
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var imports := []ast.Import
|
2020-01-18 23:26:14 +01:00
|
|
|
|
if p.tok.kind == .lpar {
|
|
|
|
|
p.check(.lpar)
|
|
|
|
|
for p.tok.kind != .rpar {
|
|
|
|
|
imports << p.parse_import()
|
2020-04-05 02:08:10 +02:00
|
|
|
|
if p.tok.kind == .comment {
|
|
|
|
|
p.comment()
|
|
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
|
}
|
|
|
|
|
p.check(.rpar)
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else {
|
2020-04-14 18:36:29 +02:00
|
|
|
|
// p.warn('`import module` has been deprecated, use `import ( module )` instead')
|
2020-01-18 23:26:14 +01:00
|
|
|
|
imports << p.parse_import()
|
|
|
|
|
}
|
|
|
|
|
return imports
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02: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-04-02 19:39:17 +02:00
|
|
|
|
pos := p.tok.position()
|
2020-01-18 23:26:14 +01:00
|
|
|
|
p.check(.key_const)
|
|
|
|
|
p.check(.lpar)
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var fields := []ast.ConstField
|
2020-01-18 23:26:14 +01:00
|
|
|
|
for p.tok.kind != .rpar {
|
2020-04-05 02:08:10 +02:00
|
|
|
|
if p.tok.kind == .comment {
|
|
|
|
|
p.comment()
|
|
|
|
|
}
|
2020-02-22 13:04:56 +01:00
|
|
|
|
name := p.prepend_mod(p.check_name())
|
2020-04-04 05:14:40 +02:00
|
|
|
|
// name := 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-03-05 12:08:43 +01:00
|
|
|
|
expr := p.expr(0)
|
2020-04-04 05:14:40 +02:00
|
|
|
|
field := ast.ConstField{
|
2020-02-03 07:02:54 +01:00
|
|
|
|
name: name
|
2020-04-04 05:14:40 +02:00
|
|
|
|
expr: expr
|
2020-04-02 21:31:36 +02:00
|
|
|
|
pos: p.tok.position()
|
2020-02-03 07:02:54 +01:00
|
|
|
|
}
|
2020-04-04 05:14:40 +02:00
|
|
|
|
fields << field
|
|
|
|
|
p.global_scope.register(field.name, field)
|
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{
|
2020-04-07 15:48:13 +02:00
|
|
|
|
pos: pos
|
2020-02-03 07:02:54 +01:00
|
|
|
|
fields: fields
|
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
|
2020-04-15 04:15:42 +02: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-04-08 01:20:55 +02:00
|
|
|
|
is_union := p.tok.kind == .key_union
|
2020-02-26 15:51:05 +01:00
|
|
|
|
if p.tok.kind == .key_struct {
|
|
|
|
|
p.check(.key_struct)
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else {
|
2020-02-26 15:51:05 +01:00
|
|
|
|
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 {
|
2020-04-07 15:48:13 +02:00
|
|
|
|
p.next() // C
|
|
|
|
|
p.next() // .
|
2020-02-10 23:19:50 +01:00
|
|
|
|
}
|
2020-04-04 14:32:42 +02:00
|
|
|
|
is_typedef := p.attr == 'typedef'
|
2020-04-08 16:54:49 +02:00
|
|
|
|
no_body := p.peek_tok.kind != .lcbr
|
|
|
|
|
if !is_c && no_body {
|
|
|
|
|
p.error('`$p.tok.lit` lacks body')
|
|
|
|
|
}
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var name := p.check_name()
|
2020-03-27 08:46:54 +01:00
|
|
|
|
// println('struct decl $name')
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var ast_fields := []ast.StructField
|
|
|
|
|
var fields := []table.Field
|
|
|
|
|
var mut_pos := -1
|
|
|
|
|
var pub_pos := -1
|
|
|
|
|
var pub_mut_pos := -1
|
2020-04-08 16:54:49 +02:00
|
|
|
|
if !no_body {
|
|
|
|
|
p.check(.lcbr)
|
|
|
|
|
for p.tok.kind != .rcbr {
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var comment := ast.Comment{}
|
2020-04-08 16:54:49 +02:00
|
|
|
|
if p.tok.kind == .comment {
|
|
|
|
|
comment = p.comment()
|
|
|
|
|
}
|
|
|
|
|
if p.tok.kind == .key_pub {
|
|
|
|
|
p.check(.key_pub)
|
|
|
|
|
if p.tok.kind == .key_mut {
|
|
|
|
|
p.check(.key_mut)
|
|
|
|
|
pub_mut_pos = fields.len
|
|
|
|
|
} else {
|
|
|
|
|
pub_pos = fields.len
|
|
|
|
|
}
|
|
|
|
|
p.check(.colon)
|
|
|
|
|
} else if p.tok.kind == .key_mut {
|
2020-02-04 12:50:58 +01:00
|
|
|
|
p.check(.key_mut)
|
2020-04-08 16:54:49 +02:00
|
|
|
|
p.check(.colon)
|
|
|
|
|
mut_pos = fields.len
|
|
|
|
|
} else if p.tok.kind == .key_global {
|
|
|
|
|
p.check(.key_global)
|
|
|
|
|
p.check(.colon)
|
2020-02-04 12:50:58 +01:00
|
|
|
|
}
|
2020-04-08 16:54:49 +02:00
|
|
|
|
field_name := p.check_name()
|
|
|
|
|
field_pos := p.tok.position()
|
|
|
|
|
// p.warn('field $field_name')
|
|
|
|
|
typ := p.parse_type()
|
|
|
|
|
/*
|
|
|
|
|
if name == '_net_module_s' {
|
2020-03-31 14:33:16 +02:00
|
|
|
|
s := p.table.get_type_symbol(typ)
|
|
|
|
|
println('XXXX' + s.str())
|
|
|
|
|
}
|
2020-04-07 15:48:13 +02:00
|
|
|
|
*/
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var default_expr := ast.Expr{}
|
|
|
|
|
var has_default_expr := false
|
2020-04-08 16:54:49 +02:00
|
|
|
|
if p.tok.kind == .assign {
|
|
|
|
|
// Default value
|
|
|
|
|
p.next()
|
2020-04-12 12:35:54 +02:00
|
|
|
|
// default_expr = p.tok.lit
|
|
|
|
|
// p.expr(0)
|
|
|
|
|
default_expr = p.expr(0)
|
|
|
|
|
match default_expr {
|
2020-04-14 18:09:59 +02:00
|
|
|
|
ast.EnumVal {
|
|
|
|
|
it.typ = typ
|
|
|
|
|
}
|
2020-04-12 12:35:54 +02:00
|
|
|
|
// TODO: implement all types??
|
|
|
|
|
else {}
|
|
|
|
|
}
|
|
|
|
|
has_default_expr = true
|
2020-04-08 16:54:49 +02:00
|
|
|
|
}
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var attr := ast.Attr{}
|
2020-04-12 17:45:04 +02:00
|
|
|
|
if p.tok.kind == .lsbr {
|
|
|
|
|
attr = p.attribute()
|
|
|
|
|
}
|
2020-04-08 16:54:49 +02:00
|
|
|
|
if p.tok.kind == .comment {
|
|
|
|
|
comment = p.comment()
|
|
|
|
|
}
|
|
|
|
|
ast_fields << ast.StructField{
|
|
|
|
|
name: field_name
|
|
|
|
|
pos: field_pos
|
|
|
|
|
typ: typ
|
|
|
|
|
comment: comment
|
|
|
|
|
default_expr: default_expr
|
2020-04-12 12:35:54 +02:00
|
|
|
|
has_default_expr: has_default_expr
|
2020-04-12 17:45:04 +02:00
|
|
|
|
attr: attr.name
|
2020-04-08 16:54:49 +02:00
|
|
|
|
}
|
|
|
|
|
fields << table.Field{
|
|
|
|
|
name: field_name
|
|
|
|
|
typ: typ
|
2020-04-12 12:35:54 +02:00
|
|
|
|
default_expr: default_expr
|
|
|
|
|
has_default_expr: has_default_expr
|
2020-04-08 16:54:49 +02:00
|
|
|
|
}
|
|
|
|
|
// println('struct field $ti.name $field_name')
|
2020-01-02 20:09:15 +01:00
|
|
|
|
}
|
2020-04-08 16:54:49 +02:00
|
|
|
|
p.check(.rcbr)
|
2019-12-30 06:16:59 +01:00
|
|
|
|
}
|
2020-02-29 11:47:47 +01:00
|
|
|
|
if is_c {
|
|
|
|
|
name = 'C.$name'
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else {
|
2020-02-29 11:47:47 +01:00
|
|
|
|
name = p.prepend_mod(name)
|
|
|
|
|
}
|
2020-02-10 08:32:08 +01:00
|
|
|
|
t := table.TypeSymbol{
|
2020-02-08 09:50:12 +01:00
|
|
|
|
kind: .struct_
|
2020-02-29 11:47:47 +01:00
|
|
|
|
name: name
|
2020-02-08 09:50:12 +01:00
|
|
|
|
info: table.Struct{
|
2020-04-08 00:59:28 +02:00
|
|
|
|
fields: fields
|
|
|
|
|
is_typedef: is_typedef
|
2020-04-08 01:20:55 +02:00
|
|
|
|
is_union: is_union
|
2020-04-08 00:59:28 +02:00
|
|
|
|
}
|
2020-02-04 17:44:39 +01:00
|
|
|
|
}
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var ret := 0
|
2020-02-08 09:50:12 +01:00
|
|
|
|
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-04-07 15:48:13 +02: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
|
2020-03-13 01:43:30 +01:00
|
|
|
|
is_c: is_c
|
2020-04-08 01:20:55 +02:00
|
|
|
|
is_union: is_union
|
2019-12-30 06:16:59 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) interface_decl() ast.InterfaceDecl {
|
2020-03-31 19:59:38 +02:00
|
|
|
|
is_pub := p.tok.kind == .key_pub
|
|
|
|
|
if is_pub {
|
|
|
|
|
p.next()
|
|
|
|
|
}
|
2020-04-07 15:48:13 +02:00
|
|
|
|
p.next() // `interface`
|
2020-03-31 19:59:38 +02:00
|
|
|
|
interface_name := p.check_name()
|
|
|
|
|
p.check(.lcbr)
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var field_names := []string
|
2020-03-31 19:59:38 +02:00
|
|
|
|
for p.tok.kind != .rcbr && p.tok.kind != .eof {
|
|
|
|
|
line_nr := p.tok.line_nr
|
|
|
|
|
name := p.check_name()
|
2020-03-31 20:26:15 +02:00
|
|
|
|
field_names << name
|
2020-03-31 19:59:38 +02:00
|
|
|
|
p.fn_args()
|
|
|
|
|
if p.tok.kind == .name && p.tok.line_nr == line_nr {
|
|
|
|
|
p.parse_type()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
p.check(.rcbr)
|
|
|
|
|
return ast.InterfaceDecl{
|
|
|
|
|
name: interface_name
|
2020-03-31 20:26:15 +02:00
|
|
|
|
field_names: field_names
|
2020-03-31 19:59:38 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02: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
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var exprs := []ast.Expr
|
2020-03-05 13:57:05 +01:00
|
|
|
|
if p.tok.kind == .rcbr {
|
2020-01-22 21:34:38 +01:00
|
|
|
|
return ast.Return{
|
|
|
|
|
pos: p.tok.position()
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-10 22:27:51 +02:00
|
|
|
|
for {
|
2020-03-05 12:08:43 +01:00
|
|
|
|
expr := p.expr(0)
|
2020-01-07 12:10:07 +01:00
|
|
|
|
exprs << expr
|
|
|
|
|
if p.tok.kind == .comma {
|
|
|
|
|
p.check(.comma)
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else {
|
2020-01-07 12:10:07 +01:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
|
stmt := ast.Return{
|
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`
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) parse_assign_lhs() []ast.Ident {
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var idents := []ast.Ident
|
2020-04-10 22:27:51 +02:00
|
|
|
|
for {
|
2020-04-15 01:45:27 +02:00
|
|
|
|
is_mut := p.tok.kind == .key_mut || p.tok.kind == .key_var
|
2020-02-28 13:29:04 +01:00
|
|
|
|
if is_mut {
|
2020-04-15 01:45:27 +02:00
|
|
|
|
p.next()
|
2020-02-28 13:29:04 +01:00
|
|
|
|
}
|
|
|
|
|
is_static := p.tok.kind == .key_static
|
|
|
|
|
if is_static {
|
|
|
|
|
p.check(.key_static)
|
|
|
|
|
}
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var ident := p.parse_ident(false)
|
2020-04-11 04:09:41 +02:00
|
|
|
|
ident.is_mut = is_mut
|
2020-02-28 13:29:04 +01:00
|
|
|
|
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-04-07 15:48:13 +02: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`
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) parse_assign_rhs() []ast.Expr {
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var exprs := []ast.Expr
|
2020-04-10 22:27:51 +02:00
|
|
|
|
for {
|
2020-03-05 12:08:43 +01:00
|
|
|
|
expr := p.expr(0)
|
2020-02-28 13:29:04 +01:00
|
|
|
|
exprs << expr
|
|
|
|
|
if p.tok.kind == .comma {
|
|
|
|
|
p.check(.comma)
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else {
|
2020-02-28 13:29:04 +01:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return exprs
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) assign_stmt() ast.Stmt {
|
2020-04-01 13:38:05 +02:00
|
|
|
|
is_static := p.tok.kind == .key_static
|
|
|
|
|
if is_static {
|
|
|
|
|
p.next()
|
|
|
|
|
}
|
2020-02-28 13:29:04 +01:00
|
|
|
|
idents := p.parse_assign_lhs()
|
2020-02-23 11:22:07 +01:00
|
|
|
|
op := p.tok.kind
|
2020-04-07 15:48:13 +02:00
|
|
|
|
p.next() // :=, =
|
2020-04-10 14:38:43 +02:00
|
|
|
|
pos := p.tok.position()
|
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-03-10 14:45:21 +01:00
|
|
|
|
for i, ident in idents {
|
2020-04-14 20:39:55 +02:00
|
|
|
|
if op == .decl_assign && scanner.contains_capital(ident.name) {
|
2020-04-15 01:45:27 +02:00
|
|
|
|
p.error_with_pos('variable names cannot contain uppercase letters, use snake_case instead',
|
|
|
|
|
ident.pos)
|
2020-04-14 20:39:55 +02:00
|
|
|
|
}
|
2020-02-28 13:29:04 +01:00
|
|
|
|
known_var := p.scope.known_var(ident.name)
|
|
|
|
|
if !is_decl && !known_var {
|
|
|
|
|
p.error('unknown variable `$ident.name`')
|
|
|
|
|
}
|
2020-02-27 11:15:30 +01:00
|
|
|
|
if is_decl && ident.kind != .blank_ident {
|
2020-04-04 14:55:40 +02:00
|
|
|
|
if ident.name.starts_with('__') {
|
|
|
|
|
p.error('variable names cannot start with `__`')
|
|
|
|
|
}
|
2020-02-27 11:12:30 +01:00
|
|
|
|
if p.scope.known_var(ident.name) {
|
|
|
|
|
p.error('redefinition of `$ident.name`')
|
|
|
|
|
}
|
2020-03-10 14:45:21 +01:00
|
|
|
|
if idents.len == exprs.len {
|
2020-04-04 05:14:40 +02:00
|
|
|
|
p.scope.register(ident.name, ast.Var{
|
2020-03-10 14:45:21 +01:00
|
|
|
|
name: ident.name
|
|
|
|
|
expr: exprs[i]
|
2020-04-11 04:09:41 +02:00
|
|
|
|
is_mut: ident.is_mut || p.inside_for
|
2020-03-10 14:45:21 +01:00
|
|
|
|
})
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else {
|
2020-04-04 05:14:40 +02:00
|
|
|
|
p.scope.register(ident.name, ast.Var{
|
2020-03-10 14:45:21 +01:00
|
|
|
|
name: ident.name
|
2020-04-11 04:09:41 +02:00
|
|
|
|
is_mut: ident.is_mut || p.inside_for
|
2020-03-10 14:45:21 +01:00
|
|
|
|
})
|
|
|
|
|
}
|
2020-02-27 11:12:30 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
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-03-31 06:34:59 +02:00
|
|
|
|
pos: pos
|
2020-04-01 13:38:05 +02:00
|
|
|
|
is_static: is_static
|
2020-02-06 17:38:02 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) global_decl() ast.GlobalDecl {
|
2020-04-08 01:00:58 +02:00
|
|
|
|
if !p.pref.translated && !p.pref.is_live && !p.builtin_mod && !p.pref.building_v && p.mod !=
|
2020-04-07 18:51:39 +02:00
|
|
|
|
'ui' && p.mod != 'gg2' && p.mod != 'uiold' && !os.getwd().contains('/volt') && !p.pref.enable_globals {
|
2020-02-03 07:02:54 +01:00
|
|
|
|
p.error('use `v --enable-globals ...` to enable globals')
|
|
|
|
|
}
|
|
|
|
|
p.next()
|
|
|
|
|
name := p.check_name()
|
2020-03-31 19:58:44 +02:00
|
|
|
|
// println(name)
|
2020-02-03 07:02:54 +01:00
|
|
|
|
typ := p.parse_type()
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var expr := ast.Expr{}
|
2020-04-07 18:51:39 +02:00
|
|
|
|
has_expr := p.tok.kind == .assign
|
|
|
|
|
if has_expr {
|
2020-02-03 07:02:54 +01:00
|
|
|
|
p.next()
|
2020-04-07 18:51:39 +02:00
|
|
|
|
expr = p.expr(0)
|
2020-02-03 07:02:54 +01:00
|
|
|
|
}
|
|
|
|
|
// p.genln(p.table.cgen_name_type_pair(name, typ))
|
|
|
|
|
/*
|
2020-04-04 05:14:40 +02: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-04-07 15:48:13 +02:00
|
|
|
|
*/
|
2020-04-04 05:14:40 +02:00
|
|
|
|
glob := ast.GlobalDecl{
|
2020-02-03 07:02:54 +01:00
|
|
|
|
name: name
|
2020-03-06 16:31:40 +01:00
|
|
|
|
typ: typ
|
2020-04-07 18:51:39 +02:00
|
|
|
|
has_expr: has_expr
|
|
|
|
|
expr: expr
|
2020-02-03 07:02:54 +01:00
|
|
|
|
}
|
2020-04-04 05:14:40 +02:00
|
|
|
|
p.global_scope.register(name, glob)
|
|
|
|
|
return glob
|
2020-02-03 07:02:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) match_expr() ast.MatchExpr {
|
2020-04-14 01:03:31 +02:00
|
|
|
|
match_first_pos := p.tok.position()
|
2020-04-15 16:23:03 +02:00
|
|
|
|
p.inside_match = true
|
2020-02-05 10:00:11 +01:00
|
|
|
|
p.check(.key_match)
|
2020-04-15 01:45:27 +02:00
|
|
|
|
is_mut := p.tok.kind in [.key_mut, .key_var]
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var is_sum_type := false
|
2020-02-12 01:16:38 +01:00
|
|
|
|
if is_mut {
|
|
|
|
|
p.next()
|
|
|
|
|
}
|
2020-03-05 12:08:43 +01:00
|
|
|
|
cond := p.expr(0)
|
2020-04-15 16:23:03 +02:00
|
|
|
|
p.inside_match = false
|
2020-02-05 10:00:11 +01:00
|
|
|
|
p.check(.lcbr)
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var branches := []ast.MatchBranch
|
2020-04-10 22:27:51 +02:00
|
|
|
|
for {
|
2020-04-14 01:03:31 +02:00
|
|
|
|
branch_first_pos := p.tok.position()
|
2020-04-07 15:48:13 +02:00
|
|
|
|
comment := p.check_comment() // comment before {}
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var exprs := []ast.Expr
|
2020-03-02 10:53:38 +01:00
|
|
|
|
p.open_scope()
|
2020-03-04 11:59:45 +01:00
|
|
|
|
// final else
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var is_else := false
|
2020-03-04 11:59:45 +01:00
|
|
|
|
if p.tok.kind == .key_else {
|
2020-04-14 01:03:31 +02:00
|
|
|
|
is_else = true
|
2020-03-04 11:59:45 +01:00
|
|
|
|
p.next()
|
2020-04-14 18:09:59 +02:00
|
|
|
|
} else if p.tok.kind == .name && (p.tok.lit in table.builtin_type_names || (p.tok.lit[0].is_capital() &&
|
|
|
|
|
!p.tok.lit.is_upper()) || p.peek_tok.kind == .dot) {
|
2020-04-07 15:48:13 +02:00
|
|
|
|
// Sum type match
|
2020-02-13 01:06:34 +01:00
|
|
|
|
// if sym.kind == .sum_type {
|
|
|
|
|
// p.warn('is sum')
|
2020-04-08 19:08:54 +02:00
|
|
|
|
// TODO `exprs << ast.Type{...}
|
2020-03-02 10:53:38 +01:00
|
|
|
|
typ := p.parse_type()
|
2020-03-20 20:22:34 +01:00
|
|
|
|
x := ast.Type{
|
2020-03-02 10:53:38 +01:00
|
|
|
|
typ: typ
|
2020-02-13 01:06:34 +01:00
|
|
|
|
}
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var expr := ast.Expr{}
|
2020-03-20 20:22:34 +01:00
|
|
|
|
expr = x
|
|
|
|
|
exprs << expr
|
2020-04-04 05:14:40 +02:00
|
|
|
|
p.scope.register('it', ast.Var{
|
2020-03-02 10:53:38 +01:00
|
|
|
|
name: 'it'
|
2020-03-16 05:02:41 +01:00
|
|
|
|
typ: table.type_to_ptr(typ)
|
2020-03-02 10:53:38 +01:00
|
|
|
|
})
|
2020-03-06 14:14:33 +01:00
|
|
|
|
// TODO
|
|
|
|
|
if p.tok.kind == .comma {
|
|
|
|
|
p.next()
|
|
|
|
|
p.parse_type()
|
|
|
|
|
}
|
2020-03-16 03:19:26 +01:00
|
|
|
|
is_sum_type = true
|
2020-04-07 15:48:13 +02:00
|
|
|
|
} else {
|
2020-02-11 00:07:01 +01:00
|
|
|
|
// Expression match
|
2020-04-10 22:27:51 +02:00
|
|
|
|
for {
|
2020-04-03 18:05:50 +02:00
|
|
|
|
p.inside_match_case = true
|
2020-03-05 12:08:43 +01:00
|
|
|
|
expr := p.expr(0)
|
2020-04-03 18:05:50 +02:00
|
|
|
|
p.inside_match_case = false
|
2020-03-04 11:59:45 +01:00
|
|
|
|
exprs << expr
|
2020-02-11 10:26:46 +01:00
|
|
|
|
if p.tok.kind != .comma {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
p.check(.comma)
|
|
|
|
|
}
|
2020-02-11 00:07:01 +01:00
|
|
|
|
}
|
2020-04-14 01:03:31 +02:00
|
|
|
|
branch_last_pos := p.tok.position()
|
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-04-14 01:03:31 +02:00
|
|
|
|
pos := token.Position{
|
2020-04-14 20:31:51 +02:00
|
|
|
|
line_nr: branch_first_pos.line_nr
|
|
|
|
|
pos: branch_first_pos.pos
|
2020-04-14 01:03:31 +02:00
|
|
|
|
len: branch_last_pos.pos - branch_first_pos.pos + branch_last_pos.len
|
|
|
|
|
}
|
2020-03-04 11:59:45 +01:00
|
|
|
|
branches << ast.MatchBranch{
|
|
|
|
|
exprs: exprs
|
2020-02-10 20:33:34 +01:00
|
|
|
|
stmts: stmts
|
2020-04-14 01:03:31 +02:00
|
|
|
|
pos: pos
|
2020-04-07 15:15:45 +02:00
|
|
|
|
comment: comment
|
2020-04-14 01:03:31 +02:00
|
|
|
|
is_else: is_else
|
2020-02-07 14:49:14 +01:00
|
|
|
|
}
|
2020-03-02 10:53:38 +01:00
|
|
|
|
p.close_scope()
|
2020-02-05 10:00:11 +01:00
|
|
|
|
if p.tok.kind == .rcbr {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-14 01:03:31 +02:00
|
|
|
|
match_last_pos := p.tok.position()
|
|
|
|
|
pos := token.Position{
|
|
|
|
|
line_nr: match_first_pos.line_nr
|
|
|
|
|
pos: match_first_pos.pos
|
|
|
|
|
len: match_last_pos.pos - match_first_pos.pos + match_last_pos.len
|
2020-04-02 16:51:16 +02:00
|
|
|
|
}
|
2020-02-05 10:00:11 +01:00
|
|
|
|
p.check(.rcbr)
|
2020-03-04 11:59:45 +01:00
|
|
|
|
return ast.MatchExpr{
|
|
|
|
|
branches: branches
|
2020-02-07 14:49:14 +01:00
|
|
|
|
cond: cond
|
2020-03-16 03:19:26 +01:00
|
|
|
|
is_sum_type: is_sum_type
|
2020-03-21 13:57:11 +01:00
|
|
|
|
pos: pos
|
2020-04-07 15:15:45 +02:00
|
|
|
|
is_mut: is_mut
|
2020-02-07 14:49:14 +01:00
|
|
|
|
}
|
2020-02-05 10:00:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) enum_decl() ast.EnumDecl {
|
2020-02-10 23:19:50 +01:00
|
|
|
|
is_pub := p.tok.kind == .key_pub
|
|
|
|
|
if is_pub {
|
|
|
|
|
p.next()
|
|
|
|
|
}
|
|
|
|
|
p.check(.key_enum)
|
2020-03-14 05:20:12 +01:00
|
|
|
|
name := p.prepend_mod(p.check_name())
|
2020-02-10 23:19:50 +01:00
|
|
|
|
p.check(.lcbr)
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var vals := []string
|
2020-04-09 19:23:49 +02:00
|
|
|
|
// mut default_exprs := []ast.Expr
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var fields := []ast.EnumField
|
2020-02-10 23:19:50 +01:00
|
|
|
|
for p.tok.kind != .eof && p.tok.kind != .rcbr {
|
2020-04-09 19:23:49 +02:00
|
|
|
|
pos := p.tok.position()
|
2020-04-10 00:09:34 +02:00
|
|
|
|
val := p.check_name()
|
2020-02-10 23:19:50 +01:00
|
|
|
|
vals << val
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var expr := ast.Expr{}
|
|
|
|
|
var has_expr := false
|
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()
|
2020-04-10 14:44:01 +02:00
|
|
|
|
expr = p.expr(0)
|
|
|
|
|
has_expr = true
|
2020-02-12 01:16:38 +01:00
|
|
|
|
}
|
2020-04-10 14:44:01 +02:00
|
|
|
|
fields << ast.EnumField{val, pos, expr, has_expr}
|
2020-04-01 12:11:00 +02:00
|
|
|
|
// Allow commas after enum, helpful for
|
|
|
|
|
// enum Color {
|
2020-04-01 13:38:05 +02:00
|
|
|
|
// r,g,b
|
2020-04-01 12:11:00 +02:00
|
|
|
|
// }
|
|
|
|
|
if p.tok.kind == .comma {
|
|
|
|
|
p.next()
|
|
|
|
|
}
|
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-03-14 05:20:12 +01:00
|
|
|
|
name: name
|
2020-02-25 13:30:43 +01:00
|
|
|
|
info: table.Enum{
|
2020-04-08 00:59:28 +02:00
|
|
|
|
vals: vals
|
|
|
|
|
}
|
2020-02-25 13:30:43 +01:00
|
|
|
|
})
|
2020-02-10 23:19:50 +01:00
|
|
|
|
return ast.EnumDecl{
|
|
|
|
|
name: name
|
|
|
|
|
is_pub: is_pub
|
2020-04-09 19:23:49 +02:00
|
|
|
|
fields: fields
|
2020-02-10 23:19:50 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) type_decl() ast.TypeDecl {
|
2020-02-10 23:19:50 +01:00
|
|
|
|
is_pub := p.tok.kind == .key_pub
|
|
|
|
|
if is_pub {
|
|
|
|
|
p.next()
|
|
|
|
|
}
|
|
|
|
|
p.check(.key_type)
|
|
|
|
|
name := p.check_name()
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var sum_variants := []table.Type
|
2020-02-10 23:19:50 +01:00
|
|
|
|
if p.tok.kind == .assign {
|
2020-04-14 18:09:59 +02:00
|
|
|
|
p.next() // TODO require `=`
|
2020-04-13 15:06:02 +02:00
|
|
|
|
}
|
|
|
|
|
if p.tok.kind == .key_fn {
|
|
|
|
|
// function type: `type mycallback fn(string, int)`
|
|
|
|
|
fn_name := p.prepend_mod(name)
|
|
|
|
|
fn_type := p.parse_fn_type(fn_name)
|
|
|
|
|
return ast.FnTypeDecl{
|
|
|
|
|
name: fn_name
|
|
|
|
|
is_pub: is_pub
|
|
|
|
|
typ: fn_type
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-14 18:09:59 +02:00
|
|
|
|
first_type := p.parse_type() // need to parse the first type before we can check if it's `type A = X | Y`
|
2020-04-13 15:06:02 +02:00
|
|
|
|
if p.tok.kind == .pipe {
|
|
|
|
|
p.check(.pipe)
|
|
|
|
|
sum_variants << first_type
|
2020-03-07 17:37:55 +01:00
|
|
|
|
// type SumType = A | B | c
|
2020-04-10 22:27:51 +02:00
|
|
|
|
for {
|
2020-03-02 06:40:18 +01:00
|
|
|
|
variant_type := p.parse_type()
|
|
|
|
|
sum_variants << variant_type
|
2020-02-10 23:19:50 +01:00
|
|
|
|
if p.tok.kind != .pipe {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
p.check(.pipe)
|
|
|
|
|
}
|
2020-03-02 06:40:18 +01:00
|
|
|
|
p.table.register_type_symbol(table.TypeSymbol{
|
|
|
|
|
kind: .sum_type
|
|
|
|
|
name: p.prepend_mod(name)
|
|
|
|
|
info: table.SumType{
|
2020-04-08 00:59:28 +02:00
|
|
|
|
variants: sum_variants
|
|
|
|
|
}
|
2020-03-02 06:40:18 +01:00
|
|
|
|
})
|
2020-03-07 17:37:55 +01:00
|
|
|
|
return ast.SumTypeDecl{
|
|
|
|
|
name: name
|
|
|
|
|
is_pub: is_pub
|
|
|
|
|
sub_types: sum_variants
|
|
|
|
|
}
|
2020-03-02 23:19:04 +01:00
|
|
|
|
}
|
|
|
|
|
// type MyType int
|
2020-04-13 15:06:02 +02:00
|
|
|
|
parent_type := first_type
|
2020-03-07 17:37:55 +01:00
|
|
|
|
pid := table.type_idx(parent_type)
|
|
|
|
|
p.table.register_type_symbol(table.TypeSymbol{
|
|
|
|
|
kind: .alias
|
|
|
|
|
name: p.prepend_mod(name)
|
|
|
|
|
parent_idx: pid
|
|
|
|
|
info: table.Alias{
|
2020-04-08 00:59:28 +02:00
|
|
|
|
foo: ''
|
|
|
|
|
}
|
2020-03-07 17:37:55 +01:00
|
|
|
|
})
|
|
|
|
|
return ast.AliasTypeDecl{
|
2020-02-10 23:19:50 +01:00
|
|
|
|
name: name
|
2020-03-07 17:37:55 +01:00
|
|
|
|
is_pub: is_pub
|
|
|
|
|
parent_type: parent_type
|
2020-02-10 23:19:50 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 04:15:42 +02:00
|
|
|
|
fn (p mut Parser) assoc() ast.Assoc {
|
2020-04-01 23:23:20 +02:00
|
|
|
|
var_name := p.check_name()
|
|
|
|
|
pos := p.tok.position()
|
2020-04-15 01:45:27 +02:00
|
|
|
|
v := p.scope.find_var(var_name) or {
|
2020-04-01 23:23:20 +02:00
|
|
|
|
p.error('unknown variable `$var_name`')
|
|
|
|
|
return ast.Assoc{}
|
|
|
|
|
}
|
|
|
|
|
// println('assoc var $name typ=$var.typ')
|
2020-04-15 01:55:51 +02:00
|
|
|
|
var fields := []string
|
|
|
|
|
var vals := []ast.Expr
|
2020-04-01 23:23:20 +02:00
|
|
|
|
p.check(.pipe)
|
2020-04-10 22:27:51 +02:00
|
|
|
|
for {
|
2020-04-01 23:23:20 +02:00
|
|
|
|
fields << p.check_name()
|
|
|
|
|
p.check(.colon)
|
|
|
|
|
expr := p.expr(0)
|
|
|
|
|
vals << expr
|
|
|
|
|
if p.tok.kind == .comma {
|
|
|
|
|
p.check(.comma)
|
|
|
|
|
}
|
|
|
|
|
if p.tok.kind == .rcbr {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ast.Assoc{
|
|
|
|
|
var_name: var_name
|
|
|
|
|
fields: fields
|
|
|
|
|
exprs: vals
|
|
|
|
|
pos: pos
|
2020-04-15 01:45:27 +02:00
|
|
|
|
typ: v.typ
|
2020-04-01 23:23:20 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-31 20:26:15 +02:00
|
|
|
|
fn (p &Parser) new_true_expr() ast.Expr {
|
|
|
|
|
return ast.BoolLiteral{
|
|
|
|
|
val: true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-26 11:21:41 +01:00
|
|
|
|
fn verror(s string) {
|
2020-04-03 17:38:41 +02:00
|
|
|
|
util.verror('parser error', s)
|
2019-12-26 11:21:41 +01:00
|
|
|
|
}
|