mirror of
https://github.com/Nexoniarz/Finch.git
synced 2026-11-03 00:44:27 +00:00
Bootstrap: the Finch compiler written in Finch
boot/ is a second Finch compiler written in Finch (lexer, parser, type checker and an LLVM IR text generator, ~3k lines). Built by the C++ compiler it compiles itself; stage 2 and stage 3 produce byte-identical IR. It covers the core language: functions, structs, arrays, str, pointers, loops and modules (no C imports, sized numbers or defer), copies values like the main compiler and leaves freeing memory to the operating system. tests/boot.sh checks the fixpoint and runs the test programs it supports. Also: shell(cmd) built-in. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
1dc976bd8f
commit
c7ddd4d8b6
8 changed files with 3153 additions and 1 deletions
94
boot/ast.fn
Normal file
94
boot/ast.fn
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
// The syntax tree of the self-hosted compiler.
|
||||
|
||||
// A type as written or resolved.
|
||||
// kind: "void" "int" "bool" "char" "float" "str" "null" "ptr" "array" "named" "struct"
|
||||
// elem: the element of an array or the target of a ptr (zero or one entry)
|
||||
// name, module: structs ("" module = the main file)
|
||||
struct Type {
|
||||
str kind
|
||||
[]Type elem
|
||||
str name
|
||||
str module
|
||||
}
|
||||
|
||||
fn simple(str kind) -> Type {
|
||||
return Type(kind: kind)
|
||||
}
|
||||
|
||||
fn arrayOf(Type t) -> Type {
|
||||
out := Type(kind: "array")
|
||||
out.elem.push(t)
|
||||
return out
|
||||
}
|
||||
|
||||
fn ptrTo(Type t) -> Type {
|
||||
out := Type(kind: "ptr")
|
||||
out.elem.push(t)
|
||||
return out
|
||||
}
|
||||
|
||||
fn same(Type a, Type b) -> bool {
|
||||
if a.kind != b.kind {
|
||||
return false
|
||||
}
|
||||
if a.kind == "struct" || a.kind == "named" {
|
||||
return a.name == b.name && a.module == b.module
|
||||
}
|
||||
if a.kind == "array" || a.kind == "ptr" {
|
||||
if a.elem.len != b.elem.len {
|
||||
return false
|
||||
}
|
||||
if a.elem.len == 0 {
|
||||
return true
|
||||
}
|
||||
return same(a.elem[0], b.elem[0])
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
fn show(Type t) -> str {
|
||||
if t.kind == "array" {
|
||||
return "[]" + show(t.elem[0])
|
||||
}
|
||||
if t.kind == "ptr" {
|
||||
if t.elem.len == 0 {
|
||||
return "ptr"
|
||||
}
|
||||
return "ptr[" + show(t.elem[0]) + "]"
|
||||
}
|
||||
if t.kind == "struct" || t.kind == "named" {
|
||||
if t.module.len > 0 {
|
||||
return t.module + "." + t.name
|
||||
}
|
||||
return t.name
|
||||
}
|
||||
if t.kind == "void" {
|
||||
return "nothing"
|
||||
}
|
||||
return t.kind
|
||||
}
|
||||
|
||||
// One node of the tree, for expressions, statements, functions and structs alike.
|
||||
// expressions: int float str char bool null var unary binary call member index array method
|
||||
// statements: decl assign expr if while for each return break continue block
|
||||
// top level: fn param struct field
|
||||
struct Node {
|
||||
str kind
|
||||
str name // identifier, operator, callee, field or method name
|
||||
str text // literal text
|
||||
Type ty // declared type (decl, param, field, fn result)
|
||||
bool flag // decl: has a type; fn: has a body
|
||||
[]Node kids
|
||||
[]str names // named arguments: Point(x: 1)
|
||||
int line
|
||||
int col
|
||||
int file
|
||||
}
|
||||
|
||||
struct Program {
|
||||
str module
|
||||
str path
|
||||
[]str imports // Finch modules
|
||||
[]Node structs
|
||||
[]Node fns
|
||||
}
|
||||
2161
boot/gen.fn
Normal file
2161
boot/gen.fn
Normal file
File diff suppressed because it is too large
Load diff
210
boot/lexer.fn
Normal file
210
boot/lexer.fn
Normal file
|
|
@ -0,0 +1,210 @@
|
|||
// The Finch lexer, written in Finch.
|
||||
// A token's kind is a word: "ident", "int", "float", "string", "char", "eof",
|
||||
// or the keyword / operator itself ("fn", "{", "+=", ...).
|
||||
|
||||
struct Token {
|
||||
str kind
|
||||
str text
|
||||
int line
|
||||
int col
|
||||
bool nl // a newline came before this token
|
||||
}
|
||||
|
||||
fn isAlpha(char c) -> bool {
|
||||
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'
|
||||
}
|
||||
|
||||
fn isDigit(char c) -> bool {
|
||||
return c >= '0' && c <= '9'
|
||||
}
|
||||
|
||||
fn isHex(char c) -> bool {
|
||||
return isDigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')
|
||||
}
|
||||
|
||||
fn isKeyword(str w) -> bool {
|
||||
keywords := ["fn", "return", "if", "else", "while", "for", "in", "break", "continue", "true", "false",
|
||||
"null", "import", "link", "struct", "defer"]
|
||||
return keywords.contains(w)
|
||||
}
|
||||
|
||||
struct Lexer {
|
||||
str src
|
||||
str path
|
||||
int i
|
||||
int line
|
||||
int col
|
||||
bool nl
|
||||
}
|
||||
|
||||
fn fail(ptr[Lexer] lx, int line, int col, str msg) {
|
||||
print(lx.path + ":" + str(line) + ":" + str(col) + ": error: " + msg)
|
||||
exit(1)
|
||||
}
|
||||
|
||||
fn peek(ptr[Lexer] lx, int off) -> char {
|
||||
if lx.i + off < lx.src.len {
|
||||
return lx.src[lx.i + off]
|
||||
}
|
||||
return char(0)
|
||||
}
|
||||
|
||||
fn advance(ptr[Lexer] lx) {
|
||||
c := lx.src[lx.i]
|
||||
if c == '\n' {
|
||||
lx.line += 1
|
||||
lx.col = 1
|
||||
lx.nl = true
|
||||
} else if int(c) & 0xC0 != 0x80 {
|
||||
lx.col += 1
|
||||
}
|
||||
lx.i += 1
|
||||
}
|
||||
|
||||
// operators, longest first
|
||||
fn operator(ptr[Lexer] lx) -> str {
|
||||
twos := ["->", "<<", ">>", "<=", ">=", "==", "!=", "&&", "||", "..", ":=", "+=", "-=", "*=", "/=", "%="]
|
||||
if lx.i + 1 < lx.src.len {
|
||||
two := lx.src.sub(lx.i, lx.i + 2)
|
||||
if twos.contains(two) {
|
||||
return two
|
||||
}
|
||||
}
|
||||
ones := "(){}[],.:+-*/%&|^~<>=!"
|
||||
c := lx.src.sub(lx.i, lx.i + 1)
|
||||
if ones.contains(c) {
|
||||
return c
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
fn escape(ptr[Lexer] lx, char e) -> char {
|
||||
if e == 'n' {
|
||||
return '\n'
|
||||
}
|
||||
if e == 't' {
|
||||
return '\t'
|
||||
}
|
||||
if e == 'r' {
|
||||
return '\r'
|
||||
}
|
||||
if e == '0' {
|
||||
return char(0)
|
||||
}
|
||||
if e == '\\' || e == '"' || e == '\'' {
|
||||
return e
|
||||
}
|
||||
fail(lx, lx.line, lx.col - 1, "unknown escape \\" + str(e))
|
||||
return e
|
||||
}
|
||||
|
||||
fn lex(str src, str path) -> []Token {
|
||||
lexer := Lexer(src: src, path: path, i: 0, line: 1, col: 1, nl: true)
|
||||
lx := addr(lexer)
|
||||
[]Token out
|
||||
while lx.i < lx.src.len {
|
||||
c := lx.src[lx.i]
|
||||
if c == ' ' || c == '\t' || c == '\n' || c == '\r' {
|
||||
advance(lx)
|
||||
continue
|
||||
}
|
||||
if c == '/' && peek(lx, 1) == '/' {
|
||||
while lx.i < lx.src.len && lx.src[lx.i] != '\n' {
|
||||
advance(lx)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if c == '/' && peek(lx, 1) == '*' {
|
||||
line := lx.line
|
||||
col := lx.col
|
||||
advance(lx)
|
||||
advance(lx)
|
||||
while lx.i < lx.src.len && !(lx.src[lx.i] == '*' && peek(lx, 1) == '/') {
|
||||
advance(lx)
|
||||
}
|
||||
if lx.i >= lx.src.len {
|
||||
fail(lx, line, col, "comment is never closed (missing */)")
|
||||
}
|
||||
advance(lx)
|
||||
advance(lx)
|
||||
continue
|
||||
}
|
||||
|
||||
t := Token(kind: "", text: "", line: lx.line, col: lx.col, nl: lx.nl)
|
||||
lx.nl = false
|
||||
start := lx.i
|
||||
if isAlpha(c) {
|
||||
while lx.i < lx.src.len && (isAlpha(lx.src[lx.i]) || isDigit(lx.src[lx.i])) {
|
||||
advance(lx)
|
||||
}
|
||||
t.text = lx.src.sub(start, lx.i)
|
||||
t.kind = "ident"
|
||||
if isKeyword(t.text) {
|
||||
t.kind = t.text
|
||||
}
|
||||
} else if isDigit(c) {
|
||||
t.kind = "int"
|
||||
if c == '0' && (peek(lx, 1) == 'x' || peek(lx, 1) == 'X') {
|
||||
advance(lx)
|
||||
advance(lx)
|
||||
while lx.i < lx.src.len && (isHex(lx.src[lx.i]) || lx.src[lx.i] == '_') {
|
||||
advance(lx)
|
||||
}
|
||||
} else {
|
||||
while lx.i < lx.src.len && (isDigit(lx.src[lx.i]) || lx.src[lx.i] == '_') {
|
||||
advance(lx)
|
||||
}
|
||||
if peek(lx, 0) == '.' && isDigit(peek(lx, 1)) {
|
||||
t.kind = "float"
|
||||
advance(lx)
|
||||
while lx.i < lx.src.len && (isDigit(lx.src[lx.i]) || lx.src[lx.i] == '_') {
|
||||
advance(lx)
|
||||
}
|
||||
}
|
||||
}
|
||||
t.text = lx.src.sub(start, lx.i).replace("_", "")
|
||||
} else if c == '"' || c == '\'' {
|
||||
quote := c
|
||||
t.kind = "string"
|
||||
if quote == '\'' {
|
||||
t.kind = "char"
|
||||
}
|
||||
advance(lx)
|
||||
[]str parts
|
||||
while peek(lx, 0) != quote {
|
||||
if peek(lx, 0) == char(0) || peek(lx, 0) == '\n' {
|
||||
if quote == '"' {
|
||||
fail(lx, t.line, t.col, "string is never closed (missing \")")
|
||||
}
|
||||
fail(lx, t.line, t.col, "char is never closed (missing ')")
|
||||
}
|
||||
ch := peek(lx, 0)
|
||||
advance(lx)
|
||||
if ch == '\\' {
|
||||
e := peek(lx, 0)
|
||||
advance(lx)
|
||||
ch = escape(lx, e)
|
||||
}
|
||||
parts.push(str(ch))
|
||||
}
|
||||
advance(lx)
|
||||
t.text = parts.join("")
|
||||
if quote == '\'' && t.text.len != 1 {
|
||||
fail(lx, t.line, t.col, "a char holds exactly one character, like 'a'")
|
||||
}
|
||||
} else {
|
||||
op := operator(lx)
|
||||
if op.len == 0 {
|
||||
fail(lx, t.line, t.col, "unexpected character '" + str(c) + "'")
|
||||
}
|
||||
for k in 0..op.len {
|
||||
advance(lx)
|
||||
}
|
||||
t.kind = op
|
||||
t.text = op
|
||||
}
|
||||
out.push(t)
|
||||
}
|
||||
out.push(Token(kind: "eof", text: "", line: lx.line, col: lx.col, nl: true))
|
||||
return out
|
||||
}
|
||||
116
boot/main.fn
Normal file
116
boot/main.fn
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
// finch-boot: the Finch compiler written in Finch.
|
||||
//
|
||||
// finch-boot build <file.fn> [-o name] compile a program
|
||||
// finch-boot ir <file.fn> print the LLVM IR
|
||||
//
|
||||
// It reads the program and the modules it imports, checks it, writes LLVM IR as text
|
||||
// and lets clang turn that into a program, linked with Finch's C runtime.
|
||||
import lexer
|
||||
import ast
|
||||
import parser
|
||||
import gen
|
||||
|
||||
fn dirOf(str path) -> str {
|
||||
i := path.len - 1
|
||||
while i >= 0 && path[i] != '/' {
|
||||
i -= 1
|
||||
}
|
||||
if i < 0 {
|
||||
return "."
|
||||
}
|
||||
return path.sub(0, i)
|
||||
}
|
||||
|
||||
fn stem(str path) -> str {
|
||||
name := path.sub(dirOf(path).len + 1, path.len)
|
||||
if dirOf(path) == "." && !path.starts_with("./") {
|
||||
name = path
|
||||
}
|
||||
dot := name.find(".")
|
||||
if dot > 0 {
|
||||
return name.sub(0, dot)
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
struct Loader {
|
||||
[]ast.Program progs
|
||||
[]str files
|
||||
[]str loaded
|
||||
}
|
||||
|
||||
fn load(ptr[Loader] l, str path, str module) {
|
||||
if !file_exists(path) {
|
||||
if module.len > 0 {
|
||||
print("error: can't find the module '" + module + "' (looked for " + path + ")")
|
||||
} else {
|
||||
print("error: can't open '" + path + "'")
|
||||
}
|
||||
exit(1)
|
||||
}
|
||||
src := read_file(path)
|
||||
file := l.files.len
|
||||
l.files.push(path)
|
||||
prog := parser.parse(lexer.lex(src, path), file, path)
|
||||
prog.module = module
|
||||
prog.path = path
|
||||
imports := prog.imports
|
||||
l.progs.push(prog)
|
||||
for name in imports {
|
||||
if !l.loaded.contains(name) {
|
||||
l.loaded.push(name)
|
||||
load(l, dirOf(path) + "/" + name + ".fn", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn usage() {
|
||||
print("finch-boot - the Finch compiler written in Finch")
|
||||
print("usage: finch-boot build <file.fn> [-o name]")
|
||||
print(" finch-boot ir <file.fn>")
|
||||
exit(1)
|
||||
}
|
||||
|
||||
fn main([]str args) -> int {
|
||||
if args.len < 3 {
|
||||
usage()
|
||||
}
|
||||
cmd := args[1]
|
||||
file := args[2]
|
||||
out := stem(file)
|
||||
if args.len == 5 && args[3] == "-o" {
|
||||
out = args[4]
|
||||
} else if args.len != 3 {
|
||||
usage()
|
||||
}
|
||||
if cmd != "build" && cmd != "ir" {
|
||||
usage()
|
||||
}
|
||||
|
||||
loader := Loader()
|
||||
load(addr(loader), file, "")
|
||||
ir := gen.generate(loader.progs, loader.files)
|
||||
if cmd == "ir" {
|
||||
print(ir)
|
||||
return 0
|
||||
}
|
||||
|
||||
// the runtime is Finch's C file; FINCH_RT can point somewhere else
|
||||
rt := dirOf(args[0]) + "/../runtime/finch_rt.c"
|
||||
if file_exists("runtime/finch_rt.c") {
|
||||
rt = "runtime/finch_rt.c"
|
||||
}
|
||||
if !file_exists(rt) {
|
||||
print("error: can't find the Finch runtime (runtime/finch_rt.c)")
|
||||
return 1
|
||||
}
|
||||
llPath := out + ".ll"
|
||||
write_file(llPath, ir)
|
||||
code := shell("clang -O2 -w '" + llPath + "' '" + rt + "' -o '" + out + "' -lm")
|
||||
shell("rm -f '" + llPath + "'")
|
||||
if code != 0 {
|
||||
print("error: clang failed")
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
501
boot/parser.fn
Normal file
501
boot/parser.fn
Normal file
|
|
@ -0,0 +1,501 @@
|
|||
// The Finch parser, written in Finch: tokens -> ast.Program.
|
||||
import lexer
|
||||
import ast
|
||||
|
||||
struct Parser {
|
||||
[]lexer.Token toks
|
||||
int i
|
||||
int depth // inside ( ) and [ ] newlines don't end anything
|
||||
int file
|
||||
str path
|
||||
}
|
||||
|
||||
fn fail(ptr[Parser] p, lexer.Token t, str msg) {
|
||||
print(p.path + ":" + str(t.line) + ":" + str(t.col) + ": error: " + msg)
|
||||
exit(1)
|
||||
}
|
||||
|
||||
fn cur(ptr[Parser] p) -> lexer.Token {
|
||||
return p.toks[p.i]
|
||||
}
|
||||
|
||||
fn curKind(ptr[Parser] p) -> str {
|
||||
return p.toks[p.i].kind
|
||||
}
|
||||
|
||||
fn peekKind(ptr[Parser] p, int off) -> str {
|
||||
k := p.i + off
|
||||
if k >= p.toks.len {
|
||||
k = p.toks.len - 1
|
||||
}
|
||||
return p.toks[k].kind
|
||||
}
|
||||
|
||||
fn at(ptr[Parser] p, str k) -> bool {
|
||||
return p.toks[p.i].kind == k
|
||||
}
|
||||
|
||||
fn next(ptr[Parser] p) -> lexer.Token {
|
||||
t := p.toks[p.i]
|
||||
p.i += 1
|
||||
return t
|
||||
}
|
||||
|
||||
fn accept(ptr[Parser] p, str k) -> bool {
|
||||
if at(p, k) {
|
||||
p.i += 1
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
fn describe(lexer.Token t) -> str {
|
||||
if t.kind == "ident" {
|
||||
return "'" + t.text + "'"
|
||||
}
|
||||
if t.kind == "eof" {
|
||||
return "end of file"
|
||||
}
|
||||
if t.kind == "int" || t.kind == "float" {
|
||||
return "a number"
|
||||
}
|
||||
if t.kind == "string" {
|
||||
return "a string"
|
||||
}
|
||||
if t.kind == "char" {
|
||||
return "a char"
|
||||
}
|
||||
return "'" + t.kind + "'"
|
||||
}
|
||||
|
||||
fn expect(ptr[Parser] p, str k, str what) -> lexer.Token {
|
||||
if !at(p, k) {
|
||||
fail(p, cur(p), "expected " + what + ", found " + describe(cur(p)))
|
||||
}
|
||||
return next(p)
|
||||
}
|
||||
|
||||
fn sameLine(ptr[Parser] p) -> bool {
|
||||
return p.depth > 0 || !p.toks[p.i].nl
|
||||
}
|
||||
|
||||
fn node(ptr[Parser] p, str kind) -> ast.Node {
|
||||
t := p.toks[p.i]
|
||||
return ast.Node(kind: kind, line: t.line, col: t.col, file: p.file)
|
||||
}
|
||||
|
||||
fn isTypeName(str n) -> bool {
|
||||
return n == "int" || n == "float" || n == "bool" || n == "char" || n == "str" || n == "ptr"
|
||||
}
|
||||
|
||||
// int, str, ptr[T], []T, Name, module.Name
|
||||
fn parseType(ptr[Parser] p) -> ast.Type {
|
||||
if accept(p, "[") {
|
||||
expect(p, "]", "']' (an array type looks like []int)")
|
||||
return ast.arrayOf(parseType(p))
|
||||
}
|
||||
if !at(p, "ident") {
|
||||
fail(p, cur(p), "unexpected " + describe(cur(p)) + " (expected a type like int, str, []int or a struct name)")
|
||||
}
|
||||
name := next(p).text
|
||||
if isTypeName(name) {
|
||||
if name == "ptr" && accept(p, "[") {
|
||||
t := ast.ptrTo(parseType(p))
|
||||
expect(p, "]", "']'")
|
||||
return t
|
||||
}
|
||||
return ast.simple(name)
|
||||
}
|
||||
t := ast.Type(kind: "named", name: name)
|
||||
if at(p, ".") && peekKind(p, 1) == "ident" {
|
||||
next(p)
|
||||
t.module = name
|
||||
t.name = next(p).text
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
fn atDeclaration(ptr[Parser] p) -> bool {
|
||||
if at(p, "[") {
|
||||
return peekKind(p, 1) == "]"
|
||||
}
|
||||
if !at(p, "ident") {
|
||||
return false
|
||||
}
|
||||
n := peekKind(p, 1)
|
||||
if n == "ident" {
|
||||
return !p.toks[p.i + 1].nl
|
||||
}
|
||||
if cur(p).text == "ptr" && n == "[" {
|
||||
return true
|
||||
}
|
||||
return n == "." && peekKind(p, 2) == "ident" && peekKind(p, 3) == "ident" && !p.toks[p.i + 3].nl
|
||||
}
|
||||
|
||||
fn endOfStatement(ptr[Parser] p) {
|
||||
if at(p, "}") || at(p, "eof") || cur(p).nl {
|
||||
return
|
||||
}
|
||||
fail(p, cur(p), "unexpected " + describe(cur(p)) + " (put each statement on its own line)")
|
||||
}
|
||||
|
||||
// ---------- top level ----------
|
||||
|
||||
fn parseProgram(ptr[Parser] p) -> ast.Program {
|
||||
prog := ast.Program()
|
||||
while !at(p, "eof") {
|
||||
if at(p, "import") {
|
||||
t := next(p)
|
||||
if !at(p, "ident") {
|
||||
fail(p, t, "the self-hosted compiler only imports Finch modules (import name)")
|
||||
}
|
||||
prog.imports.push(next(p).text)
|
||||
} else if at(p, "struct") {
|
||||
prog.structs.push(parseStruct(p))
|
||||
} else {
|
||||
prog.fns.push(parseFn(p))
|
||||
}
|
||||
}
|
||||
return prog
|
||||
}
|
||||
|
||||
fn parseStruct(ptr[Parser] p) -> ast.Node {
|
||||
s := node(p, "struct")
|
||||
expect(p, "struct", "'struct'")
|
||||
s.name = expect(p, "ident", "a struct name").text
|
||||
expect(p, "{", "'{'")
|
||||
while !at(p, "}") {
|
||||
if at(p, "eof") {
|
||||
fail(p, cur(p), "a struct is never closed (missing '}')")
|
||||
}
|
||||
f := node(p, "field")
|
||||
if !atDeclaration(p) {
|
||||
fail(p, cur(p), "unexpected " + describe(cur(p)) + " (a field looks like: int x)")
|
||||
}
|
||||
f.ty = parseType(p)
|
||||
f.name = expect(p, "ident", "a field name").text
|
||||
if accept(p, "=") {
|
||||
f.kids.push(parseExpr(p))
|
||||
}
|
||||
s.kids.push(f)
|
||||
endOfStatement(p)
|
||||
}
|
||||
next(p)
|
||||
return s
|
||||
}
|
||||
|
||||
fn parseFn(ptr[Parser] p) -> ast.Node {
|
||||
f := node(p, "fn")
|
||||
expect(p, "fn", "'fn', 'struct' or 'import'")
|
||||
f.name = expect(p, "ident", "a function name").text
|
||||
expect(p, "(", "'('")
|
||||
p.depth += 1
|
||||
if !at(p, ")") {
|
||||
while true {
|
||||
prm := node(p, "param")
|
||||
prm.ty = parseType(p)
|
||||
prm.name = expect(p, "ident", "a parameter name").text
|
||||
f.kids.push(prm)
|
||||
if !accept(p, ",") {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
p.depth -= 1
|
||||
expect(p, ")", "')'")
|
||||
f.ty = ast.simple("void")
|
||||
if accept(p, "->") {
|
||||
f.ty = parseType(p)
|
||||
}
|
||||
f.kids.push(parseBlock(p))
|
||||
return f
|
||||
}
|
||||
|
||||
// ---------- statements ----------
|
||||
|
||||
fn parseBlock(ptr[Parser] p) -> ast.Node {
|
||||
blk := node(p, "block")
|
||||
expect(p, "{", "'{'")
|
||||
saved := p.depth
|
||||
p.depth = 0
|
||||
while !at(p, "}") {
|
||||
if at(p, "eof") {
|
||||
fail(p, cur(p), "a '{' is never closed (missing '}')")
|
||||
}
|
||||
blk.kids.push(parseStatement(p))
|
||||
endOfStatement(p)
|
||||
}
|
||||
p.depth = saved
|
||||
next(p)
|
||||
return blk
|
||||
}
|
||||
|
||||
fn parseStatement(ptr[Parser] p) -> ast.Node {
|
||||
k := curKind(p)
|
||||
if k == "if" {
|
||||
return parseIf(p)
|
||||
}
|
||||
if k == "while" {
|
||||
s := node(p, "while")
|
||||
next(p)
|
||||
s.kids.push(parseExpr(p))
|
||||
s.kids.push(parseBlock(p))
|
||||
return s
|
||||
}
|
||||
if k == "for" {
|
||||
s := node(p, "for")
|
||||
next(p)
|
||||
s.name = expect(p, "ident", "a loop variable name").text
|
||||
expect(p, "in", "'in'")
|
||||
s.kids.push(parseExpr(p))
|
||||
if accept(p, "..") {
|
||||
s.kids.push(parseExpr(p))
|
||||
} else {
|
||||
s.kind = "each"
|
||||
}
|
||||
s.kids.push(parseBlock(p))
|
||||
return s
|
||||
}
|
||||
if k == "return" {
|
||||
s := node(p, "return")
|
||||
next(p)
|
||||
if !at(p, "}") && !cur(p).nl {
|
||||
s.kids.push(parseExpr(p))
|
||||
}
|
||||
return s
|
||||
}
|
||||
if k == "break" || k == "continue" {
|
||||
s := node(p, k)
|
||||
next(p)
|
||||
return s
|
||||
}
|
||||
if k == "{" {
|
||||
return parseBlock(p)
|
||||
}
|
||||
if k == "fn" || k == "struct" || k == "import" || k == "defer" || k == "link" {
|
||||
fail(p, cur(p), "'" + k + "' can't be used here in the self-hosted compiler")
|
||||
}
|
||||
|
||||
if atDeclaration(p) {
|
||||
s := node(p, "decl")
|
||||
s.flag = true
|
||||
s.ty = parseType(p)
|
||||
s.name = expect(p, "ident", "a variable name").text
|
||||
if accept(p, "=") {
|
||||
s.kids.push(parseExpr(p))
|
||||
}
|
||||
return s
|
||||
}
|
||||
if at(p, "ident") && peekKind(p, 1) == ":=" {
|
||||
s := node(p, "decl")
|
||||
s.name = next(p).text
|
||||
next(p)
|
||||
s.kids.push(parseExpr(p))
|
||||
return s
|
||||
}
|
||||
|
||||
start := cur(p)
|
||||
e := parseExpr(p)
|
||||
ops := ["=", "+=", "-=", "*=", "/=", "%="]
|
||||
if ops.contains(curKind(p)) && sameLine(p) {
|
||||
if e.kind != "var" && e.kind != "member" && e.kind != "index" {
|
||||
fail(p, start, "only a variable, a field, an element or p.value can be assigned to")
|
||||
}
|
||||
s := ast.Node(kind: "assign", line: start.line, col: start.col, file: p.file)
|
||||
s.name = next(p).kind
|
||||
s.kids.push(e)
|
||||
s.kids.push(parseExpr(p))
|
||||
return s
|
||||
}
|
||||
if e.kind != "call" && e.kind != "method" {
|
||||
fail(p, start, "this value is computed but never used")
|
||||
}
|
||||
s := ast.Node(kind: "expr", line: start.line, col: start.col, file: p.file)
|
||||
s.kids.push(e)
|
||||
return s
|
||||
}
|
||||
|
||||
fn parseIf(ptr[Parser] p) -> ast.Node {
|
||||
s := node(p, "if")
|
||||
expect(p, "if", "'if'")
|
||||
s.kids.push(parseExpr(p))
|
||||
s.kids.push(parseBlock(p))
|
||||
if accept(p, "else") {
|
||||
if at(p, "if") {
|
||||
s.kids.push(parseIf(p))
|
||||
} else {
|
||||
s.kids.push(parseBlock(p))
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// ---------- expressions ----------
|
||||
|
||||
fn binaryLevel(str op) -> int {
|
||||
if op == "||" {
|
||||
return 1
|
||||
}
|
||||
if op == "&&" {
|
||||
return 2
|
||||
}
|
||||
if op == "==" || op == "!=" || op == "<" || op == "<=" || op == ">" || op == ">=" {
|
||||
return 3
|
||||
}
|
||||
if op == "+" || op == "-" || op == "|" || op == "^" {
|
||||
return 4
|
||||
}
|
||||
if op == "*" || op == "/" || op == "%" || op == "<<" || op == ">>" || op == "&" {
|
||||
return 5
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
fn parseExpr(ptr[Parser] p) -> ast.Node {
|
||||
return parseBinary(p, 1)
|
||||
}
|
||||
|
||||
// precedence climbing; all binary operators are left-associative
|
||||
fn parseBinary(ptr[Parser] p, int level) -> ast.Node {
|
||||
if level > 5 {
|
||||
return parseUnary(p)
|
||||
}
|
||||
left := parseBinary(p, level + 1)
|
||||
while sameLine(p) && binaryLevel(curKind(p)) == level {
|
||||
e := node(p, "binary")
|
||||
e.name = next(p).kind
|
||||
e.kids.push(left)
|
||||
e.kids.push(parseBinary(p, level + 1))
|
||||
left = e
|
||||
}
|
||||
return left
|
||||
}
|
||||
|
||||
fn parseUnary(ptr[Parser] p) -> ast.Node {
|
||||
if at(p, "-") || at(p, "!") || at(p, "~") {
|
||||
e := node(p, "unary")
|
||||
e.name = next(p).kind
|
||||
e.kids.push(parseUnary(p))
|
||||
return e
|
||||
}
|
||||
return parsePostfix(p)
|
||||
}
|
||||
|
||||
fn parseArgs(ptr[Parser] p, ptr[ast.Node] call) {
|
||||
expect(p, "(", "'('")
|
||||
p.depth += 1
|
||||
if !at(p, ")") {
|
||||
while true {
|
||||
name := ""
|
||||
if at(p, "ident") && peekKind(p, 1) == ":" {
|
||||
name = next(p).text
|
||||
next(p)
|
||||
}
|
||||
call.names.push(name)
|
||||
call.kids.push(parseExpr(p))
|
||||
if !accept(p, ",") {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
p.depth -= 1
|
||||
expect(p, ")", "')'")
|
||||
}
|
||||
|
||||
fn parsePostfix(ptr[Parser] p) -> ast.Node {
|
||||
e := parsePrimary(p)
|
||||
while sameLine(p) {
|
||||
if at(p, ".") {
|
||||
m := node(p, "member")
|
||||
next(p)
|
||||
m.name = expect(p, "ident", "a name after '.'").text
|
||||
m.kids.push(e)
|
||||
if at(p, "(") && !cur(p).nl {
|
||||
m.kind = "method"
|
||||
parseArgs(p, addr(m))
|
||||
}
|
||||
e = m
|
||||
} else if at(p, "[") {
|
||||
ix := node(p, "index")
|
||||
next(p)
|
||||
p.depth += 1
|
||||
ix.kids.push(e)
|
||||
ix.kids.push(parseExpr(p))
|
||||
p.depth -= 1
|
||||
expect(p, "]", "']'")
|
||||
e = ix
|
||||
} else {
|
||||
return e
|
||||
}
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
fn parsePrimary(ptr[Parser] p) -> ast.Node {
|
||||
t := cur(p)
|
||||
e := node(p, "")
|
||||
k := t.kind
|
||||
if k == "int" || k == "float" || k == "char" {
|
||||
next(p)
|
||||
e.kind = k
|
||||
e.text = t.text
|
||||
return e
|
||||
}
|
||||
if k == "string" {
|
||||
next(p)
|
||||
e.kind = "str"
|
||||
e.text = t.text
|
||||
return e
|
||||
}
|
||||
if k == "true" || k == "false" {
|
||||
next(p)
|
||||
e.kind = "bool"
|
||||
e.text = k
|
||||
return e
|
||||
}
|
||||
if k == "null" {
|
||||
next(p)
|
||||
e.kind = "null"
|
||||
return e
|
||||
}
|
||||
if k == "(" {
|
||||
next(p)
|
||||
p.depth += 1
|
||||
inner := parseExpr(p)
|
||||
p.depth -= 1
|
||||
expect(p, ")", "')'")
|
||||
return inner
|
||||
}
|
||||
if k == "[" {
|
||||
next(p)
|
||||
e.kind = "array"
|
||||
p.depth += 1
|
||||
while !at(p, "]") {
|
||||
e.kids.push(parseExpr(p))
|
||||
if !accept(p, ",") {
|
||||
break
|
||||
}
|
||||
}
|
||||
p.depth -= 1
|
||||
expect(p, "]", "']'")
|
||||
return e
|
||||
}
|
||||
if k == "ident" {
|
||||
next(p)
|
||||
e.name = t.text
|
||||
if at(p, "(") && !cur(p).nl {
|
||||
e.kind = "call"
|
||||
parseArgs(p, addr(e))
|
||||
return e
|
||||
}
|
||||
e.kind = "var"
|
||||
return e
|
||||
}
|
||||
fail(p, t, "unexpected " + describe(t) + " (expected a value)")
|
||||
return e
|
||||
}
|
||||
|
||||
fn parse([]lexer.Token toks, int file, str path) -> ast.Program {
|
||||
parser := Parser(toks: toks, i: 0, depth: 0, file: file, path: path)
|
||||
return parseProgram(addr(parser))
|
||||
}
|
||||
Loading…
Reference in a new issue