Compare commits
52 Commits
e68331fe0a
...
0808bcbc87
Author | SHA1 | Date | |
---|---|---|---|
|
0808bcbc87 | ||
|
47ff6b3cb5 | ||
|
e6061becc0 | ||
|
c8af776b15 | ||
|
b9767d0d7d | ||
|
df173a0096 | ||
|
9e799c23ba | ||
|
e52d0bf515 | ||
|
a03f570266 | ||
|
48e2d9a683 | ||
|
e40b8ece3b | ||
|
899a4df55e | ||
|
284d7ce383 | ||
|
6162d05b60 | ||
|
77cdfc229f | ||
|
856e74cb5e | ||
|
59956903f2 | ||
|
0a2f06f598 | ||
|
ec8ae05018 | ||
|
e4af5beb1c | ||
|
a1d6661a6b | ||
|
cac61ba093 | ||
|
e18ddbded9 | ||
|
b00df64f55 | ||
|
d6fcc65392 | ||
|
b5141e27d6 | ||
|
97117827c6 | ||
|
fb0bf29826 | ||
|
8ceaa734d2 | ||
|
df41da84b4 | ||
|
9ec1e00afa | ||
|
630420b114 | ||
|
856a0808de | ||
|
96595d8fb6 | ||
|
009095f771 | ||
|
5b4bb6606e | ||
|
7c5a08664a | ||
|
81859306b3 | ||
|
b365a3fec7 | ||
|
37ce12b6d8 | ||
|
4193971303 | ||
|
7282a38a08 | ||
|
16164c2235 | ||
|
f2c9cf20cb | ||
|
c9cfc467b0 | ||
|
3383921c6b | ||
|
7a7e4ec0f2 | ||
|
bd698629ff | ||
|
82de5c6e27 | ||
|
9540dc70f2 | ||
|
ba09919aa1 | ||
|
d8f6c41f04 |
60
Cargo.lock
generated
60
Cargo.lock
generated
@ -60,7 +60,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "690a62be8920ccf773ee00ef0968649b0e724cda8bd5b12286302b4ae955fdf5"
|
||||
dependencies = [
|
||||
"backtrace-sys",
|
||||
"cfg-if",
|
||||
"cfg-if 0.1.10",
|
||||
"libc",
|
||||
"rustc-demangle",
|
||||
]
|
||||
@ -119,6 +119,12 @@ version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "cgmath"
|
||||
version = "0.16.1"
|
||||
@ -161,7 +167,7 @@ version = "0.6.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "04973fa96e96579258a5091af6003abde64af786b860f18622b82e026cca60e6"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"cfg-if 0.1.10",
|
||||
"lazy_static 1.4.0",
|
||||
]
|
||||
|
||||
@ -355,7 +361,7 @@ version = "0.4.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"cfg-if 0.1.10",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -410,7 +416,7 @@ checksum = "6c722bee1037d430d0f8e687bbdbf222f27cc6e4e68d5caf630857bb2b6dbdce"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"cc",
|
||||
"cfg-if",
|
||||
"cfg-if 0.1.10",
|
||||
"libc",
|
||||
"void",
|
||||
]
|
||||
@ -428,7 +434,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
"version_check",
|
||||
"version_check 0.1.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -567,9 +573,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.4"
|
||||
version = "1.0.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "afdc77cc74ec70ed262262942ebb7dac3d479e9e5cfa2da1841c0806f6cdabcc"
|
||||
checksum = "edc3358ebc67bc8b7fa0c007f945b0b18226f78437d61bec735a9eb96b61ee70"
|
||||
dependencies = [
|
||||
"unicode-xid 0.2.0",
|
||||
]
|
||||
@ -585,11 +591,11 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.2"
|
||||
version = "1.0.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
|
||||
checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.4",
|
||||
"proc-macro2 1.0.30",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -848,6 +854,7 @@ dependencies = [
|
||||
"schala-repl",
|
||||
"stopwatch",
|
||||
"take_mut",
|
||||
"test-case",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -905,9 +912,9 @@ version = "1.0.101"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4b133a43a1ecd55d4086bd5b4dc6c1751c68b1bfbeba7a5040442022c7e7c02e"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.4",
|
||||
"quote 1.0.2",
|
||||
"syn 1.0.5",
|
||||
"proc-macro2 1.0.30",
|
||||
"quote 1.0.10",
|
||||
"syn 1.0.80",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -964,12 +971,12 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.5"
|
||||
version = "1.0.80"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "66850e97125af79138385e9b88339cbcd037e3f28ceab8c5ad98e64f0f1f80bf"
|
||||
checksum = "d010a1623fbd906d51d650a9916aaefc05ffa0e4053ff7fe601167f3e715d194"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.4",
|
||||
"quote 1.0.2",
|
||||
"proc-macro2 1.0.30",
|
||||
"quote 1.0.10",
|
||||
"unicode-xid 0.2.0",
|
||||
]
|
||||
|
||||
@ -1003,6 +1010,19 @@ dependencies = [
|
||||
"phf_codegen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "test-case"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3b114ece25254e97bf48dd4bfc2a12bad0647adacfe4cae1247a9ca6ad302cec"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"proc-macro2 1.0.30",
|
||||
"quote 1.0.10",
|
||||
"syn 1.0.80",
|
||||
"version_check 0.9.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread_local"
|
||||
version = "0.3.6"
|
||||
@ -1057,6 +1077,12 @@ version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
|
||||
|
||||
[[package]]
|
||||
name = "void"
|
||||
version = "1.0.2"
|
||||
|
10
TODO.md
10
TODO.md
@ -1,11 +1,21 @@
|
||||
# Immediate TODOs / General Code Cleanup
|
||||
|
||||
## Testing
|
||||
|
||||
* Get a test library for running many unit tests working
|
||||
* Write a human-readable display of the AST
|
||||
* Make an automatic (macro-based?) system for numbering compiler errors, this should be every type of error
|
||||
|
||||
## Symbols
|
||||
|
||||
* Add some good printf-debugging impls for SymbolTable-related items
|
||||
|
||||
* the symbol table should probably *only* be for global definitions (maybe rename it to reflect this?)
|
||||
* dealing with variable lookup w/in functions/closures should probably happen in AST -> ReducedAST
|
||||
* b/c that's where we go from a string name to a canonical ID (for e.g. 2nd param in 3rd enclosing scope)
|
||||
|
||||
* In fact to prove this works, the symbol table shoudl _parallelize_ the process of checking subscopes for local items
|
||||
|
||||
* Old notes on a plan of attack:
|
||||
|
||||
1. modify visitor so it can handle scopes
|
||||
|
@ -17,3 +17,6 @@ assert_matches = "1.5"
|
||||
|
||||
schala-lang-codegen = { path = "../codegen" }
|
||||
schala-repl = { path = "../../schala-repl" }
|
||||
|
||||
[dev-dependencies]
|
||||
test-case = "1.2.0"
|
||||
|
@ -1,13 +1,15 @@
|
||||
#![allow(clippy::upper_case_acronyms)]
|
||||
#![allow(clippy::enum_variant_names)]
|
||||
|
||||
use std::rc::Rc;
|
||||
use std::fmt;
|
||||
|
||||
mod visitor;
|
||||
mod operators;
|
||||
|
||||
pub use operators::{PrefixOp, BinOp};
|
||||
pub use visitor::{walk_ast, walk_block, ASTVisitor};
|
||||
pub use visitor::*;
|
||||
|
||||
use std::rc::Rc;
|
||||
use crate::derivative::Derivative;
|
||||
use crate::tokenizing::Location;
|
||||
|
||||
@ -15,7 +17,7 @@ use crate::tokenizing::Location;
|
||||
/// the u32 index limits the size of an AST to 2^32 nodes.
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Clone, Default)]
|
||||
pub struct ItemId {
|
||||
idx: u32,
|
||||
pub idx: u32,
|
||||
}
|
||||
|
||||
impl ItemId {
|
||||
@ -24,6 +26,12 @@ impl ItemId {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ItemId {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "ItemId:{}", self.idx)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ItemIdStore {
|
||||
last_idx: u32
|
||||
}
|
||||
|
@ -80,8 +80,8 @@ fn binop_precedences(s: &str) -> i32 {
|
||||
"<" => 20,
|
||||
"<=" => 20,
|
||||
"==" => 40,
|
||||
"=" => 10,
|
||||
"<=>" => 30,
|
||||
"=" => 5, // Assignment shoudl have highest precedence
|
||||
_ => default,
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +1,19 @@
|
||||
use crate::ast::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Recursion {
|
||||
Continue,
|
||||
Stop
|
||||
}
|
||||
|
||||
pub trait ASTVisitor: Sized {
|
||||
fn expression(&mut self, _expression: &Expression) {}
|
||||
fn expression_post(&mut self, _expression: &Expression) {}
|
||||
fn expression(&mut self, _expression: &Expression) -> Recursion { Recursion::Continue }
|
||||
fn declaration(&mut self, _declaration: &Declaration, _id: &ItemId) -> Recursion { Recursion::Continue }
|
||||
|
||||
fn declaration(&mut self, _declaration: &Declaration) {}
|
||||
fn declaration_post(&mut self, _declaration: &Declaration) {}
|
||||
fn import(&mut self, _import: &ImportSpecifier) -> Recursion { Recursion::Continue }
|
||||
fn module(&mut self, _module: &ModuleSpecifier) -> Recursion { Recursion::Continue }
|
||||
|
||||
fn import(&mut self, _import: &ImportSpecifier) {}
|
||||
fn module(&mut self, _module: &ModuleSpecifier) {}
|
||||
fn module_post(&mut self, _module: &ModuleSpecifier) {}
|
||||
|
||||
fn pattern(&mut self, _pat: &Pattern) {}
|
||||
fn pattern_post(&mut self, _pat: &Pattern) {}
|
||||
fn pattern(&mut self, _pat: &Pattern) -> Recursion { Recursion::Continue }
|
||||
}
|
||||
|
||||
pub fn walk_ast<V: ASTVisitor>(v: &mut V, ast: &AST) {
|
||||
@ -27,121 +28,121 @@ pub fn walk_block<V: ASTVisitor>(v: &mut V, block: &Block) {
|
||||
walk_expression(v, expr);
|
||||
}
|
||||
Declaration(ref decl) => {
|
||||
walk_declaration(v, decl);
|
||||
walk_declaration(v, decl, &statement.id);
|
||||
}
|
||||
Import(ref import_spec) => v.import(import_spec),
|
||||
Import(ref import_spec) => {
|
||||
v.import(import_spec);
|
||||
},
|
||||
Module(ref module_spec) => {
|
||||
v.module(module_spec);
|
||||
walk_block(v, &module_spec.contents);
|
||||
v.module_post(module_spec);
|
||||
if let Recursion::Continue = v.module(module_spec) {
|
||||
walk_block(v, &module_spec.contents);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn walk_declaration<V: ASTVisitor>(v: &mut V, decl: &Declaration) {
|
||||
pub fn walk_declaration<V: ASTVisitor>(v: &mut V, decl: &Declaration, id: &ItemId) {
|
||||
use Declaration::*;
|
||||
|
||||
v.declaration(decl);
|
||||
|
||||
match decl {
|
||||
FuncDecl(_sig, block) => {
|
||||
walk_block(v, block);
|
||||
}
|
||||
Binding {
|
||||
name: _,
|
||||
constant: _,
|
||||
type_anno: _,
|
||||
expr,
|
||||
} => {
|
||||
walk_expression(v, expr);
|
||||
}
|
||||
_ => (),
|
||||
};
|
||||
v.declaration_post(decl);
|
||||
}
|
||||
|
||||
fn walk_expression<V: ASTVisitor>(v: &mut V, expr: &Expression) {
|
||||
use ExpressionKind::*;
|
||||
|
||||
v.expression(expr);
|
||||
|
||||
match &expr.kind {
|
||||
NatLiteral(_) | FloatLiteral(_) | StringLiteral(_) | BoolLiteral(_) | Value(_) => (),
|
||||
BinExp(_, lhs, rhs) => {
|
||||
walk_expression(v, &lhs);
|
||||
walk_expression(v, &rhs);
|
||||
}
|
||||
PrefixExp(_, arg) => {
|
||||
walk_expression(v, &arg);
|
||||
}
|
||||
TupleLiteral(exprs) => {
|
||||
for expr in exprs {
|
||||
walk_expression(v, &expr);
|
||||
if let Recursion::Continue = v.declaration(decl, id) {
|
||||
match decl {
|
||||
FuncDecl(_sig, block) => {
|
||||
walk_block(v, block);
|
||||
}
|
||||
}
|
||||
NamedStruct { name: _, fields } => {
|
||||
for (_, expr) in fields.iter() {
|
||||
Binding {
|
||||
name: _,
|
||||
constant: _,
|
||||
type_anno: _,
|
||||
expr,
|
||||
} => {
|
||||
walk_expression(v, expr);
|
||||
}
|
||||
}
|
||||
Call { f, arguments } => {
|
||||
walk_expression(v, &f);
|
||||
for arg in arguments.iter() {
|
||||
match arg {
|
||||
InvocationArgument::Positional(expr) => walk_expression(v, expr),
|
||||
InvocationArgument::Keyword { expr, .. } => walk_expression(v, expr), //TODO maybe I can combine this pattern
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
Index { indexee, indexers } => {
|
||||
walk_expression(v, &indexee);
|
||||
for indexer in indexers.iter() {
|
||||
walk_expression(v, indexer);
|
||||
}
|
||||
}
|
||||
IfExpression {
|
||||
discriminator,
|
||||
body,
|
||||
} => {
|
||||
if let Some(d) = discriminator.as_ref() {
|
||||
walk_expression(v, &d);
|
||||
}
|
||||
walk_if_expr_body(v, &body.as_ref());
|
||||
}
|
||||
WhileExpression { condition, body } => {
|
||||
if let Some(d) = condition.as_ref() {
|
||||
walk_expression(v, d);
|
||||
}
|
||||
walk_block(v, &body);
|
||||
}
|
||||
ForExpression { enumerators, body } => {
|
||||
for enumerator in enumerators {
|
||||
walk_expression(v, &enumerator.generator);
|
||||
}
|
||||
match body.as_ref() {
|
||||
ForBody::MonadicReturn(expr) => walk_expression(v, expr),
|
||||
ForBody::StatementBlock(block) => walk_block(v, block),
|
||||
};
|
||||
}
|
||||
Lambda {
|
||||
params: _,
|
||||
type_anno: _,
|
||||
body,
|
||||
} => {
|
||||
walk_block(v, &body);
|
||||
}
|
||||
ListLiteral(exprs) => {
|
||||
for expr in exprs {
|
||||
walk_expression(v, &expr);
|
||||
}
|
||||
}
|
||||
};
|
||||
v.expression_post(expr);
|
||||
_ => (),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
fn walk_if_expr_body<V: ASTVisitor>(v: &mut V, body: &IfExpressionBody) {
|
||||
pub fn walk_expression<V: ASTVisitor>(v: &mut V, expr: &Expression) {
|
||||
use ExpressionKind::*;
|
||||
|
||||
if let Recursion::Continue = v.expression(expr) {
|
||||
match &expr.kind {
|
||||
NatLiteral(_) | FloatLiteral(_) | StringLiteral(_) | BoolLiteral(_) | Value(_) => (),
|
||||
BinExp(_, lhs, rhs) => {
|
||||
walk_expression(v, &lhs);
|
||||
walk_expression(v, &rhs);
|
||||
}
|
||||
PrefixExp(_, arg) => {
|
||||
walk_expression(v, &arg);
|
||||
}
|
||||
TupleLiteral(exprs) => {
|
||||
for expr in exprs {
|
||||
walk_expression(v, &expr);
|
||||
}
|
||||
}
|
||||
NamedStruct { name: _, fields } => {
|
||||
for (_, expr) in fields.iter() {
|
||||
walk_expression(v, expr);
|
||||
}
|
||||
}
|
||||
Call { f, arguments } => {
|
||||
walk_expression(v, &f);
|
||||
for arg in arguments.iter() {
|
||||
match arg {
|
||||
InvocationArgument::Positional(expr) => walk_expression(v, expr),
|
||||
InvocationArgument::Keyword { expr, .. } => walk_expression(v, expr), //TODO maybe I can combine this pattern
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
Index { indexee, indexers } => {
|
||||
walk_expression(v, &indexee);
|
||||
for indexer in indexers.iter() {
|
||||
walk_expression(v, indexer);
|
||||
}
|
||||
}
|
||||
IfExpression {
|
||||
discriminator,
|
||||
body,
|
||||
} => {
|
||||
if let Some(d) = discriminator.as_ref() {
|
||||
walk_expression(v, &d);
|
||||
}
|
||||
walk_if_expr_body(v, &body.as_ref());
|
||||
}
|
||||
WhileExpression { condition, body } => {
|
||||
if let Some(d) = condition.as_ref() {
|
||||
walk_expression(v, d);
|
||||
}
|
||||
walk_block(v, &body);
|
||||
}
|
||||
ForExpression { enumerators, body } => {
|
||||
for enumerator in enumerators {
|
||||
walk_expression(v, &enumerator.generator);
|
||||
}
|
||||
match body.as_ref() {
|
||||
ForBody::MonadicReturn(expr) => walk_expression(v, expr),
|
||||
ForBody::StatementBlock(block) => walk_block(v, block),
|
||||
};
|
||||
}
|
||||
Lambda {
|
||||
params: _,
|
||||
type_anno: _,
|
||||
body,
|
||||
} => {
|
||||
walk_block(v, &body);
|
||||
}
|
||||
ListLiteral(exprs) => {
|
||||
for expr in exprs {
|
||||
walk_expression(v, &expr);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn walk_if_expr_body<V: ASTVisitor>(v: &mut V, body: &IfExpressionBody) {
|
||||
use IfExpressionBody::*;
|
||||
|
||||
match body {
|
||||
@ -177,36 +178,38 @@ fn walk_if_expr_body<V: ASTVisitor>(v: &mut V, body: &IfExpressionBody) {
|
||||
Condition::Expression(ref expr) => {
|
||||
walk_expression(v, expr);
|
||||
}
|
||||
_ => (),
|
||||
Condition::Else => (),
|
||||
}
|
||||
if let Some(ref guard) = arm.guard {
|
||||
walk_expression(v, &guard);
|
||||
}
|
||||
walk_block(v, &arm.body);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn walk_pattern<V: ASTVisitor>(v: &mut V, pat: &Pattern) {
|
||||
pub fn walk_pattern<V: ASTVisitor>(v: &mut V, pat: &Pattern) {
|
||||
use Pattern::*;
|
||||
|
||||
v.pattern(pat);
|
||||
|
||||
match pat {
|
||||
TuplePattern(patterns) => {
|
||||
for pat in patterns {
|
||||
walk_pattern(v, pat);
|
||||
if let Recursion::Continue = v.pattern(pat) {
|
||||
match pat {
|
||||
TuplePattern(patterns) => {
|
||||
for pat in patterns {
|
||||
walk_pattern(v, pat);
|
||||
}
|
||||
}
|
||||
}
|
||||
TupleStruct(_, patterns) => {
|
||||
for pat in patterns {
|
||||
walk_pattern(v, pat);
|
||||
TupleStruct(_, patterns) => {
|
||||
for pat in patterns {
|
||||
walk_pattern(v, pat);
|
||||
}
|
||||
}
|
||||
}
|
||||
Record(_, name_and_patterns) => {
|
||||
for (_, pat) in name_and_patterns {
|
||||
walk_pattern(v, pat);
|
||||
Record(_, name_and_patterns) => {
|
||||
for (_, pat) in name_and_patterns {
|
||||
walk_pattern(v, pat);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
};
|
||||
|
||||
v.pattern_post(pat);
|
||||
_ => (),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -1,444 +0,0 @@
|
||||
use std::rc::Rc;
|
||||
use std::fmt::Write;
|
||||
use std::io;
|
||||
|
||||
use crate::util::ScopeStack;
|
||||
use crate::reduced_ast::{BoundVars, ReducedAST, Stmt, Expr, Lit, Func, Alternative, Subpattern};
|
||||
use crate::builtin::Builtin;
|
||||
|
||||
mod test;
|
||||
|
||||
pub struct State<'a> {
|
||||
values: ScopeStack<'a, Rc<String>, ValueEntry>,
|
||||
}
|
||||
|
||||
impl<'a> State<'a> {
|
||||
pub fn new() -> State<'a> {
|
||||
let values = ScopeStack::new(Some("global".to_string()));
|
||||
State { values }
|
||||
}
|
||||
|
||||
#[allow(clippy::ptr_arg)]
|
||||
fn new_frame(&'a self, items: &'a [Node], bound_vars: &BoundVars) -> State<'a> {
|
||||
let mut inner_state = State {
|
||||
values: self.values.new_scope(None),
|
||||
};
|
||||
for (bound_var, val) in bound_vars.iter().zip(items.iter()) {
|
||||
if let Some(bv) = bound_var.as_ref() {
|
||||
inner_state.values.insert(bv.clone(), ValueEntry::Binding { constant: true, val: val.clone() });
|
||||
}
|
||||
}
|
||||
inner_state
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum Node {
|
||||
Expr(Expr),
|
||||
PrimObject {
|
||||
name: Rc<String>,
|
||||
tag: usize,
|
||||
items: Vec<Node>,
|
||||
},
|
||||
PrimTuple {
|
||||
items: Vec<Node>
|
||||
}
|
||||
}
|
||||
|
||||
fn paren_wrapped_vec(terms: impl Iterator<Item=String>) -> String {
|
||||
let mut buf = String::new();
|
||||
write!(buf, "(").unwrap();
|
||||
for term in terms.map(Some).intersperse(None) {
|
||||
match term {
|
||||
Some(e) => write!(buf, "{}", e).unwrap(),
|
||||
None => write!(buf, ", ").unwrap(),
|
||||
};
|
||||
}
|
||||
write!(buf, ")").unwrap();
|
||||
buf
|
||||
}
|
||||
|
||||
|
||||
impl Node {
|
||||
fn to_repl(&self) -> String {
|
||||
match self {
|
||||
Node::Expr(e) => e.to_repl(),
|
||||
Node::PrimObject { name, items, .. } if items.is_empty() => format!("{}", name),
|
||||
Node::PrimObject { name, items, .. } => format!("{}{}", name, paren_wrapped_vec(items.iter().map(|x| x.to_repl()))),
|
||||
Node::PrimTuple { items } => paren_wrapped_vec(items.iter().map(|x| x.to_repl())),
|
||||
}
|
||||
}
|
||||
fn is_true(&self) -> bool {
|
||||
matches!(self, Node::Expr(Expr::Lit(crate::reduced_ast::Lit::Bool(true))))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum ValueEntry {
|
||||
Binding {
|
||||
constant: bool,
|
||||
val: /*FullyEvaluatedExpr*/ Node, //TODO make this use a subtype to represent fully evaluatedness
|
||||
}
|
||||
}
|
||||
|
||||
type EvalResult<T> = Result<T, String>;
|
||||
|
||||
impl Expr {
|
||||
#[allow(clippy::wrong_self_convention)]
|
||||
fn to_node(self) -> Node {
|
||||
Node::Expr(self)
|
||||
}
|
||||
fn to_repl(&self) -> String {
|
||||
use self::Lit::*;
|
||||
use self::Func::*;
|
||||
|
||||
match self {
|
||||
Expr::Lit(ref l) => match l {
|
||||
Nat(n) => format!("{}", n),
|
||||
Int(i) => format!("{}", i),
|
||||
Float(f) => format!("{}", f),
|
||||
Bool(b) => format!("{}", b),
|
||||
StringLit(s) => format!("\"{}\"", s),
|
||||
},
|
||||
Expr::Func(f) => match f {
|
||||
BuiltIn(builtin) => format!("<built-in function '{:?}'>", builtin),
|
||||
UserDefined { name: None, .. } => "<function>".to_string(),
|
||||
UserDefined { name: Some(name), .. } => format!("<function '{}'>", name),
|
||||
},
|
||||
Expr::Constructor { type_name, arity, .. } => {
|
||||
format!("<constructor for `{}` arity {}>", type_name, arity)
|
||||
},
|
||||
Expr::Tuple(exprs) => paren_wrapped_vec(exprs.iter().map(|x| x.to_repl())),
|
||||
_ => format!("{:?}", self),
|
||||
}
|
||||
}
|
||||
|
||||
fn replace_conditional_target_sigil(self, replacement: &Expr) -> Expr {
|
||||
use self::Expr::*;
|
||||
|
||||
match self {
|
||||
ConditionalTargetSigilValue => replacement.clone(),
|
||||
Lit(_) | Func(_) | Sym(_) | Constructor { .. } |
|
||||
CaseMatch { .. } | UnimplementedSigilValue | ReductionError(_) => self,
|
||||
Tuple(exprs) => Tuple(exprs.into_iter().map(|e| e.replace_conditional_target_sigil(replacement)).collect()),
|
||||
Call { f, args } => {
|
||||
let new_args = args.into_iter().map(|e| e.replace_conditional_target_sigil(replacement)).collect();
|
||||
Call { f, args: new_args }
|
||||
},
|
||||
Conditional { .. } => panic!("Dunno if I need this, but if so implement"),
|
||||
Assign { .. } => panic!("I'm pretty sure I don't need this"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> State<'a> {
|
||||
pub fn evaluate(&mut self, ast: ReducedAST, repl: bool) -> Vec<Result<String, String>> {
|
||||
let mut acc = vec![];
|
||||
|
||||
// handle prebindings
|
||||
for statement in ast.0.iter() {
|
||||
self.prebinding(statement);
|
||||
}
|
||||
|
||||
for statement in ast.0 {
|
||||
match self.statement(statement) {
|
||||
Ok(Some(ref output)) if repl => {
|
||||
acc.push(Ok(output.to_repl()))
|
||||
},
|
||||
Ok(_) => (),
|
||||
Err(error) => {
|
||||
acc.push(Err(format!("Runtime error: {}", error)));
|
||||
return acc;
|
||||
},
|
||||
}
|
||||
}
|
||||
acc
|
||||
}
|
||||
|
||||
fn prebinding(&mut self, stmt: &Stmt) {
|
||||
match stmt {
|
||||
Stmt::PreBinding { name, func } => {
|
||||
let v_entry = ValueEntry::Binding { constant: true, val: Node::Expr(Expr::Func(func.clone())) };
|
||||
self.values.insert(name.clone(), v_entry);
|
||||
},
|
||||
Stmt::Expr(_expr) => {
|
||||
//TODO have this support things like nested function defs
|
||||
|
||||
},
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
|
||||
fn statement(&mut self, stmt: Stmt) -> EvalResult<Option<Node>> {
|
||||
match stmt {
|
||||
Stmt::Binding { name, constant, expr } => {
|
||||
let val = self.expression(Node::Expr(expr))?;
|
||||
self.values.insert(name, ValueEntry::Binding { constant, val });
|
||||
Ok(None)
|
||||
},
|
||||
Stmt::Expr(expr) => Ok(Some(self.expression(expr.to_node())?)),
|
||||
Stmt::PreBinding {..} | Stmt::Noop => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
fn block(&mut self, stmts: Vec<Stmt>) -> EvalResult<Node> {
|
||||
let mut ret = None;
|
||||
for stmt in stmts {
|
||||
ret = self.statement(stmt)?;
|
||||
}
|
||||
Ok(ret.unwrap_or_else(|| Node::Expr(Expr::unit())))
|
||||
}
|
||||
|
||||
fn expression(&mut self, node: Node) -> EvalResult<Node> {
|
||||
use self::Expr::*;
|
||||
match node {
|
||||
t @ Node::PrimTuple { .. } => Ok(t),
|
||||
obj @ Node::PrimObject { .. } => Ok(obj),
|
||||
Node::Expr(expr) => match expr {
|
||||
literal @ Lit(_) => Ok(Node::Expr(literal)),
|
||||
Call { box f, args } => self.call_expression(f, args),
|
||||
Sym(name) => Ok(match self.values.lookup(&name) {
|
||||
Some(ValueEntry::Binding { val, .. }) => val.clone(),
|
||||
None => return Err(format!("Could not look up symbol {}", name))
|
||||
}),
|
||||
Constructor { arity, ref name, tag, .. } if arity == 0 => Ok(Node::PrimObject { name: name.clone(), tag, items: vec![] }),
|
||||
constructor @ Constructor { .. } => Ok(Node::Expr(constructor)),
|
||||
func @ Func(_) => Ok(Node::Expr(func)),
|
||||
Tuple(exprs) => {
|
||||
let nodes = exprs.into_iter().map(|expr| self.expression(Node::Expr(expr))).collect::<Result<Vec<Node>,_>>()?;
|
||||
Ok(Node::PrimTuple { items: nodes })
|
||||
},
|
||||
Conditional { box cond, then_clause, else_clause } => self.conditional(cond, then_clause, else_clause),
|
||||
Assign { box val, box expr } => self.assign_expression(val, expr),
|
||||
CaseMatch { box cond, alternatives } => self.case_match_expression(cond, alternatives),
|
||||
ConditionalTargetSigilValue => Ok(Node::Expr(ConditionalTargetSigilValue)),
|
||||
UnimplementedSigilValue => Err("Sigil value eval not implemented".to_string()),
|
||||
ReductionError(err) => Err(format!("Reduction error: {}", err)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn call_expression(&mut self, f: Expr, args: Vec<Expr>) -> EvalResult<Node> {
|
||||
use self::Expr::*;
|
||||
match self.expression(Node::Expr(f))? {
|
||||
Node::Expr(Constructor { type_name, name, tag, arity }) => self.apply_data_constructor(type_name, name, tag, arity, args),
|
||||
Node::Expr(Func(f)) => self.apply_function(f, args),
|
||||
other => return Err(format!("Tried to call {:?} which is not a function or data constructor", other)),
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_data_constructor(&mut self, _type_name: Rc<String>, name: Rc<String>, tag: usize, arity: usize, args: Vec<Expr>) -> EvalResult<Node> {
|
||||
if arity != args.len() {
|
||||
return Err(format!("Data constructor {} requires {} arg(s)", name, arity));
|
||||
}
|
||||
|
||||
let evaled_args = args.into_iter().map(|expr| self.expression(Node::Expr(expr))).collect::<Result<Vec<Node>,_>>()?;
|
||||
//let evaled_args = vec![];
|
||||
Ok(Node::PrimObject {
|
||||
name,
|
||||
items: evaled_args,
|
||||
tag
|
||||
})
|
||||
}
|
||||
|
||||
fn apply_function(&mut self, f: Func, args: Vec<Expr>) -> EvalResult<Node> {
|
||||
match f {
|
||||
Func::BuiltIn(builtin) => Ok(self.apply_builtin(builtin, args)?),
|
||||
Func::UserDefined { params, body, name } => {
|
||||
|
||||
if params.len() != args.len() {
|
||||
return Err(format!("calling a {}-argument function with {} args", params.len(), args.len()))
|
||||
}
|
||||
let mut func_state = State {
|
||||
values: self.values.new_scope(name.map(|n| format!("{}", n))),
|
||||
};
|
||||
for (param, val) in params.into_iter().zip(args.into_iter()) {
|
||||
let val = func_state.expression(Node::Expr(val))?;
|
||||
func_state.values.insert(param, ValueEntry::Binding { constant: true, val });
|
||||
}
|
||||
// TODO figure out function return semantics
|
||||
func_state.block(body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_builtin(&mut self, builtin: Builtin, args: Vec<Expr>) -> EvalResult<Node> {
|
||||
use self::Expr::*;
|
||||
use self::Lit::*;
|
||||
use Builtin::*;
|
||||
|
||||
let evaled_args: Result<Vec<Node>, String> = args.into_iter().map(|arg| self.expression(arg.to_node()))
|
||||
.collect();
|
||||
let evaled_args = evaled_args?;
|
||||
|
||||
Ok(match (builtin, evaled_args.as_slice()) {
|
||||
(FieldAccess, &[Node::PrimObject { .. }]) => {
|
||||
//TODO implement field access
|
||||
unimplemented!()
|
||||
},
|
||||
(binop, &[Node::Expr(ref lhs), Node::Expr(ref rhs)]) => match (binop, lhs, rhs) {
|
||||
/* binops */
|
||||
(Add, Lit(Nat(l)), Lit(Nat(r))) => Lit(Nat(l + r)),
|
||||
(Concatenate, Lit(StringLit(ref s1)), Lit(StringLit(ref s2))) => Lit(StringLit(Rc::new(format!("{}{}", s1, s2)))),
|
||||
(Subtract, Lit(Nat(l)), Lit(Nat(r))) => Lit(Nat(l - r)),
|
||||
(Multiply, Lit(Nat(l)), Lit(Nat(r))) => Lit(Nat(l * r)),
|
||||
(Divide, Lit(Nat(l)), Lit(Nat(r))) => Lit(Float((*l as f64)/ (*r as f64))),
|
||||
(Quotient, Lit(Nat(l)), Lit(Nat(r))) => if *r == 0 {
|
||||
return Err("Divide-by-zero error".to_string());
|
||||
} else {
|
||||
Lit(Nat(l / r))
|
||||
},
|
||||
(Modulo, Lit(Nat(l)), Lit(Nat(r))) => Lit(Nat(l % r)),
|
||||
(Exponentiation, Lit(Nat(l)), Lit(Nat(r))) => Lit(Nat(l ^ r)),
|
||||
(BitwiseAnd, Lit(Nat(l)), Lit(Nat(r))) => Lit(Nat(l & r)),
|
||||
(BitwiseOr, Lit(Nat(l)), Lit(Nat(r))) => Lit(Nat(l | r)),
|
||||
|
||||
/* comparisons */
|
||||
(Equality, Lit(Nat(l)), Lit(Nat(r))) => Lit(Bool(l == r)),
|
||||
(Equality, Lit(Int(l)), Lit(Int(r))) => Lit(Bool(l == r)),
|
||||
(Equality, Lit(Float(l)), Lit(Float(r))) => Lit(Bool(l == r)),
|
||||
(Equality, Lit(Bool(l)), Lit(Bool(r))) => Lit(Bool(l == r)),
|
||||
(Equality, Lit(StringLit(ref l)), Lit(StringLit(ref r))) => Lit(Bool(l == r)),
|
||||
|
||||
(LessThan, Lit(Nat(l)), Lit(Nat(r))) => Lit(Bool(l < r)),
|
||||
(LessThan, Lit(Int(l)), Lit(Int(r))) => Lit(Bool(l < r)),
|
||||
(LessThan, Lit(Float(l)), Lit(Float(r))) => Lit(Bool(l < r)),
|
||||
|
||||
(LessThanOrEqual, Lit(Nat(l)), Lit(Nat(r))) => Lit(Bool(l <= r)),
|
||||
(LessThanOrEqual, Lit(Int(l)), Lit(Int(r))) => Lit(Bool(l <= r)),
|
||||
(LessThanOrEqual, Lit(Float(l)), Lit(Float(r))) => Lit(Bool(l <= r)),
|
||||
|
||||
(GreaterThan, Lit(Nat(l)), Lit(Nat(r))) => Lit(Bool(l > r)),
|
||||
(GreaterThan, Lit(Int(l)), Lit(Int(r))) => Lit(Bool(l > r)),
|
||||
(GreaterThan, Lit(Float(l)), Lit(Float(r))) => Lit(Bool(l > r)),
|
||||
|
||||
(GreaterThanOrEqual, Lit(Nat(l)), Lit(Nat(r))) => Lit(Bool(l >= r)),
|
||||
(GreaterThanOrEqual, Lit(Int(l)), Lit(Int(r))) => Lit(Bool(l >= r)),
|
||||
(GreaterThanOrEqual, Lit(Float(l)), Lit(Float(r))) => Lit(Bool(l >= r)),
|
||||
_ => return Err("No valid binop".to_string())
|
||||
}.to_node(),
|
||||
(prefix, &[Node::Expr(ref arg)]) => match (prefix, arg) {
|
||||
(BooleanNot, Lit(Bool(true))) => Lit(Bool(false)),
|
||||
(BooleanNot, Lit(Bool(false))) => Lit(Bool(true)),
|
||||
(Negate, Lit(Nat(n))) => Lit(Int(-(*n as i64))),
|
||||
(Negate, Lit(Int(n))) => Lit(Int(-(*n as i64))),
|
||||
(Increment, Lit(Int(n))) => Lit(Int(*n)),
|
||||
(Increment, Lit(Nat(n))) => Lit(Nat(*n)),
|
||||
_ => return Err("No valid prefix op".to_string())
|
||||
}.to_node(),
|
||||
|
||||
/* builtin functions */
|
||||
(IOPrint, &[ref anything]) => {
|
||||
print!("{}", anything.to_repl());
|
||||
Expr::unit().to_node()
|
||||
},
|
||||
(IOPrintLn, &[ref anything]) => {
|
||||
println!("{}", anything.to_repl());
|
||||
Expr::unit().to_node()
|
||||
},
|
||||
(IOGetLine, &[]) => {
|
||||
let mut buf = String::new();
|
||||
io::stdin().read_line(&mut buf).expect("Error readling line in 'getline'");
|
||||
Lit(StringLit(Rc::new(buf.trim().to_string()))).to_node()
|
||||
},
|
||||
(x, args) => return Err(format!("bad or unimplemented builtin {:?} | {:?}", x, args)),
|
||||
})
|
||||
}
|
||||
|
||||
fn conditional(&mut self, cond: Expr, then_clause: Vec<Stmt>, else_clause: Vec<Stmt>) -> EvalResult<Node> {
|
||||
let cond = self.expression(Node::Expr(cond))?;
|
||||
Ok(match cond {
|
||||
Node::Expr(Expr::Lit(Lit::Bool(true))) => self.block(then_clause)?,
|
||||
Node::Expr(Expr::Lit(Lit::Bool(false))) => self.block(else_clause)?,
|
||||
_ => return Err("Conditional with non-boolean condition".to_string())
|
||||
})
|
||||
}
|
||||
|
||||
fn assign_expression(&mut self, val: Expr, expr: Expr) -> EvalResult<Node> {
|
||||
let name = match val {
|
||||
Expr::Sym(name) => name,
|
||||
_ => return Err("Trying to assign to a non-value".to_string()),
|
||||
};
|
||||
|
||||
let constant = match self.values.lookup(&name) {
|
||||
None => return Err(format!("Constant {} is undefined", name)),
|
||||
Some(ValueEntry::Binding { constant, .. }) => *constant,
|
||||
};
|
||||
if constant {
|
||||
return Err(format!("trying to update {}, a non-mutable binding", name));
|
||||
}
|
||||
let val = self.expression(Node::Expr(expr))?;
|
||||
self.values.insert(name, ValueEntry::Binding { constant: false, val });
|
||||
Ok(Node::Expr(Expr::unit()))
|
||||
}
|
||||
|
||||
fn guard_passes(&mut self, guard: &Option<Expr>, cond: &Node) -> EvalResult<bool> {
|
||||
if let Some(ref guard_expr) = guard {
|
||||
let guard_expr = match cond {
|
||||
Node::Expr(ref e) => guard_expr.clone().replace_conditional_target_sigil(e),
|
||||
_ => guard_expr.clone()
|
||||
};
|
||||
Ok(self.expression(guard_expr.to_node())?.is_true())
|
||||
} else {
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
||||
fn case_match_expression(&mut self, cond: Expr, alternatives: Vec<Alternative>) -> EvalResult<Node> {
|
||||
|
||||
//TODO need to handle recursive subpatterns
|
||||
let all_subpatterns_pass = |state: &mut State, subpatterns: &Vec<Option<Subpattern>>, items: &Vec<Node>| -> EvalResult<bool> {
|
||||
if subpatterns.is_empty() {
|
||||
return Ok(true)
|
||||
}
|
||||
|
||||
if items.len() != subpatterns.len() {
|
||||
return Err(format!("Subpattern length isn't correct items {} subpatterns {}", items.len(), subpatterns.len()));
|
||||
}
|
||||
|
||||
for (maybe_subp, cond) in subpatterns.iter().zip(items.iter()) {
|
||||
if let Some(subp) = maybe_subp {
|
||||
if !state.guard_passes(&subp.guard, cond)? {
|
||||
return Ok(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(true)
|
||||
};
|
||||
|
||||
let cond = self.expression(Node::Expr(cond))?;
|
||||
for alt in alternatives {
|
||||
// no matter what type of condition we have, ignore alternative if the guard evaluates false
|
||||
if !self.guard_passes(&alt.matchable.guard, &cond)? {
|
||||
continue;
|
||||
}
|
||||
|
||||
match cond {
|
||||
Node::PrimObject { ref tag, ref items, .. } => {
|
||||
if alt.matchable.tag.map(|t| t == *tag).unwrap_or(true) {
|
||||
let mut inner_state = self.new_frame(items, &alt.matchable.bound_vars);
|
||||
if all_subpatterns_pass(&mut inner_state, &alt.matchable.subpatterns, items)? {
|
||||
return inner_state.block(alt.item);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
},
|
||||
Node::PrimTuple { ref items } => {
|
||||
let mut inner_state = self.new_frame(items, &alt.matchable.bound_vars);
|
||||
if all_subpatterns_pass(&mut inner_state, &alt.matchable.subpatterns, items)? {
|
||||
return inner_state.block(alt.item);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
},
|
||||
Node::Expr(ref _e) => {
|
||||
if alt.matchable.tag.is_none() {
|
||||
return self.block(alt.item)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(format!("{:?} failed pattern match", cond))
|
||||
}
|
||||
}
|
@ -1,262 +0,0 @@
|
||||
#![cfg(test)]
|
||||
|
||||
use crate::symbol_table::SymbolTable;
|
||||
use crate::reduced_ast::reduce;
|
||||
use crate::eval::State;
|
||||
|
||||
fn evaluate_all_outputs(input: &str) -> Vec<Result<String, String>> {
|
||||
let ast = crate::util::quick_ast(input);
|
||||
|
||||
let mut symbol_table = SymbolTable::new();
|
||||
symbol_table.process_ast(&ast).unwrap();
|
||||
|
||||
let reduced = reduce(&ast, &symbol_table);
|
||||
|
||||
let mut state = State::new();
|
||||
|
||||
state.evaluate(reduced, true)
|
||||
}
|
||||
|
||||
macro_rules! test_in_fresh_env {
|
||||
($string:expr, $correct:expr) => {
|
||||
{
|
||||
let all_output = evaluate_all_outputs($string);
|
||||
let output = &all_output.last().unwrap();
|
||||
assert_eq!(**output, Ok($correct.to_string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_basic_eval() {
|
||||
test_in_fresh_env!("1 + 2", "3");
|
||||
test_in_fresh_env!("let mut a = 1; a = 2", "()");
|
||||
/*
|
||||
test_in_fresh_env!("let mut a = 1; a = 2; a", "2");
|
||||
test_in_fresh_env!(r#"("a", 1 + 2)"#, r#"("a", 3)"#);
|
||||
*/
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn op_eval() {
|
||||
test_in_fresh_env!("- 13", "-13");
|
||||
test_in_fresh_env!("10 - 2", "8");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn function_eval() {
|
||||
test_in_fresh_env!("fn oi(x) { x + 1 }; oi(4)", "5");
|
||||
test_in_fresh_env!("fn oi(x) { x + 1 }; oi(1+2)", "4");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scopes() {
|
||||
let scope_ok = r#"
|
||||
let a = 20
|
||||
fn haha() {
|
||||
let a = 10
|
||||
a
|
||||
}
|
||||
haha()
|
||||
"#;
|
||||
test_in_fresh_env!(scope_ok, "10");
|
||||
let scope_ok = r#"
|
||||
let a = 20
|
||||
fn queque() {
|
||||
let a = 10
|
||||
a
|
||||
}
|
||||
a
|
||||
"#;
|
||||
test_in_fresh_env!(scope_ok, "20");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn if_is_patterns() {
|
||||
let source = r#"
|
||||
type Option<T> = Some(T) | None
|
||||
let x = Option::Some(9); if x is Option::Some(q) then { q } else { 0 }"#;
|
||||
test_in_fresh_env!(source, "9");
|
||||
|
||||
let source = r#"
|
||||
type Option<T> = Some(T) | None
|
||||
let x = Option::None; if x is Option::Some(q) then { q } else { 0 }"#;
|
||||
test_in_fresh_env!(source, "0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_if_matching() {
|
||||
let source = r#"
|
||||
type Option<T> = Some(T) | None
|
||||
let a = Option::None
|
||||
if a { is Option::None then 4, is Option::Some(x) then x }
|
||||
"#;
|
||||
test_in_fresh_env!(source, "4");
|
||||
|
||||
let source = r#"
|
||||
type Option<T> = Some(T) | None
|
||||
let a = Option::Some(99)
|
||||
if a { is Option::None then 4, is Option::Some(x) then x }
|
||||
"#;
|
||||
test_in_fresh_env!(source, "99");
|
||||
|
||||
let source = r#"
|
||||
let a = 10
|
||||
if a { is 10 then "x", is 4 then "y" }
|
||||
"#;
|
||||
test_in_fresh_env!(source, "\"x\"");
|
||||
|
||||
let source = r#"
|
||||
let a = 10
|
||||
if a { is 15 then "x", is 10 then "y" }
|
||||
"#;
|
||||
test_in_fresh_env!(source, "\"y\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn string_pattern() {
|
||||
let source = r#"
|
||||
let a = "foo"
|
||||
if a { is "foo" then "x", is _ then "y" }
|
||||
"#;
|
||||
test_in_fresh_env!(source, "\"x\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn boolean_pattern() {
|
||||
let source = r#"
|
||||
let a = true
|
||||
if a {
|
||||
is true then "x",
|
||||
is false then "y"
|
||||
}
|
||||
"#;
|
||||
test_in_fresh_env!(source, "\"x\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn boolean_pattern_2() {
|
||||
let source = r#"
|
||||
let a = false
|
||||
if a { is true then "x", is false then "y" }
|
||||
"#;
|
||||
test_in_fresh_env!(source, "\"y\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ignore_pattern() {
|
||||
let source = r#"
|
||||
type Option<T> = Some(T) | None
|
||||
if Option::Some(10) {
|
||||
is _ then "hella"
|
||||
}
|
||||
"#;
|
||||
test_in_fresh_env!(source, "\"hella\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tuple_pattern() {
|
||||
let source = r#"
|
||||
if (1, 2) {
|
||||
is (1, x) then x,
|
||||
is _ then 99
|
||||
}
|
||||
"#;
|
||||
test_in_fresh_env!(source, 2);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn tuple_pattern_2() {
|
||||
let source = r#"
|
||||
if (1, 2) {
|
||||
is (10, x) then x,
|
||||
is (y, x) then x + y
|
||||
}
|
||||
"#;
|
||||
test_in_fresh_env!(source, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tuple_pattern_3() {
|
||||
let source = r#"
|
||||
if (1, 5) {
|
||||
is (10, x) then x,
|
||||
is (1, x) then x
|
||||
}
|
||||
"#;
|
||||
test_in_fresh_env!(source, 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tuple_pattern_4() {
|
||||
let source = r#"
|
||||
if (1, 5) {
|
||||
is (10, x) then x,
|
||||
is (1, x) then x,
|
||||
}
|
||||
"#;
|
||||
test_in_fresh_env!(source, 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prim_obj_pattern() {
|
||||
let source = r#"
|
||||
type Stuff = Mulch(Nat) | Jugs(Nat, String) | Mardok
|
||||
let a = Stuff::Mulch(20)
|
||||
let b = Stuff::Jugs(1, "haha")
|
||||
let c = Stuff::Mardok
|
||||
|
||||
let x = if a {
|
||||
is Stuff::Mulch(20) then "x",
|
||||
is _ then "ERR"
|
||||
}
|
||||
|
||||
let y = if b {
|
||||
is Stuff::Mulch(n) then "ERR",
|
||||
is Stuff::Jugs(2, _) then "ERR",
|
||||
is Stuff::Jugs(1, s) then s,
|
||||
is _ then "ERR",
|
||||
}
|
||||
|
||||
let z = if c {
|
||||
is Stuff::Jugs(_, _) then "ERR",
|
||||
is Stuff::Mardok then "NIGH",
|
||||
is _ then "ERR",
|
||||
}
|
||||
|
||||
(x, y, z)
|
||||
"#;
|
||||
test_in_fresh_env!(source, r#"("x", "haha", "NIGH")"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic_lambda_syntax() {
|
||||
let source = r#"
|
||||
let q = \(x, y) { x * y }
|
||||
let x = q(5,2)
|
||||
let y = \(m, n, o) { m + n + o }(1,2,3)
|
||||
(x, y)
|
||||
"#;
|
||||
test_in_fresh_env!(source, r"(10, 6)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lambda_syntax_2() {
|
||||
let source = r#"
|
||||
fn milta() {
|
||||
\(x) { x + 33 }
|
||||
}
|
||||
milta()(10)
|
||||
"#;
|
||||
test_in_fresh_env!(source, "43");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_all() {
|
||||
let source = r#"
|
||||
type Option<T> = Some(T) | None
|
||||
import Option::*
|
||||
let x = Some(9); if x is Some(q) then { q } else { 0 }"#;
|
||||
test_in_fresh_env!(source, "9");
|
||||
}
|
@ -23,8 +23,10 @@ mod tokenizing;
|
||||
mod symbol_table;
|
||||
mod builtin;
|
||||
mod error;
|
||||
mod eval;
|
||||
mod reduced_ast;
|
||||
//mod eval;
|
||||
//mod reduced_ast;
|
||||
mod reduced_ir;
|
||||
mod tree_walk_eval;
|
||||
|
||||
mod schala;
|
||||
|
||||
|
@ -830,6 +830,55 @@ fn if_expr() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn advanced_if_expr() {
|
||||
|
||||
parse_test_wrap_ast! {
|
||||
r#"
|
||||
if (45, "panda", false, 2.2) {
|
||||
is (49, "pablo", _, 28.4) then "no"
|
||||
is (_, "panda", _, -2.2) then "yes"
|
||||
is _ then "maybe"
|
||||
}"#,
|
||||
exst!(
|
||||
IfExpression {
|
||||
discriminator: Some(bx!(ex!(s r#"(45, "panda", false, 2.2)"#))),
|
||||
body: bx!(IfExpressionBody::CondList(vec![
|
||||
ConditionArm {
|
||||
condition: Condition::Pattern(Pattern::TuplePattern(
|
||||
vec![
|
||||
Pattern::Literal(PatternLiteral::NumPattern { neg: false, num: NatLiteral(49) }),
|
||||
Pattern::Literal(PatternLiteral::StringPattern(rc!(pablo))),
|
||||
Pattern::Ignored,
|
||||
Pattern::Literal(PatternLiteral::NumPattern { neg: false, num: FloatLiteral(28.4) }),
|
||||
]
|
||||
)),
|
||||
guard: None,
|
||||
body: vec![exst!(s r#""no""#)],
|
||||
},
|
||||
ConditionArm {
|
||||
condition: Condition::Pattern(Pattern::TuplePattern(
|
||||
vec![
|
||||
Pattern::Ignored,
|
||||
Pattern::Literal(PatternLiteral::StringPattern(rc!(panda))),
|
||||
Pattern::Ignored,
|
||||
Pattern::Literal(PatternLiteral::NumPattern { neg: true, num: FloatLiteral(2.2) }),
|
||||
]
|
||||
)),
|
||||
guard: None,
|
||||
body: vec![exst!(s r#""yes""#)],
|
||||
},
|
||||
ConditionArm {
|
||||
condition: Condition::Pattern(Pattern::Ignored),
|
||||
guard: None,
|
||||
body: vec![exst!(s r#""maybe""#)],
|
||||
},
|
||||
]))
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn modules() {
|
||||
parse_test_wrap_ast! {
|
||||
@ -890,4 +939,3 @@ fn annotations() {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -1,514 +0,0 @@
|
||||
//! # Reduced AST
|
||||
//! The reduced AST is a minimal AST designed to be built from the full AST after all possible
|
||||
//! static checks have been done. Consequently, the AST reduction phase does very little error
|
||||
//! checking itself - any errors should ideally be caught either by an earlier phase, or are
|
||||
//! runtime errors that the evaluator should handle. That said, becuase it does do table lookups
|
||||
//! that can in principle fail [especially at the moment with most static analysis not yet complete],
|
||||
//! there is an Expr variant `ReductionError` to handle these cases.
|
||||
//!
|
||||
//! A design decision to make - should the ReducedAST types contain all information about
|
||||
//! type/layout necessary for the evaluator to work? If so, then the evaluator should not
|
||||
//! have access to the symbol table at all and ReducedAST should carry that information. If not,
|
||||
//! then ReducedAST shouldn't be duplicating information that can be queried at runtime from the
|
||||
//! symbol table. But I think the former might make sense since ultimately the bytecode will be
|
||||
//! built from the ReducedAST.
|
||||
|
||||
use std::convert::TryFrom;
|
||||
use std::rc::Rc;
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::ast::*;
|
||||
use crate::builtin::Builtin;
|
||||
use crate::symbol_table::{Symbol, SymbolSpec, SymbolTable};
|
||||
use crate::util::deref_optional_box;
|
||||
|
||||
mod types;
|
||||
pub use types::*;
|
||||
|
||||
pub fn reduce(ast: &AST, symbol_table: &SymbolTable) -> ReducedAST {
|
||||
let mut reducer = Reducer { symbol_table };
|
||||
reducer.ast(ast)
|
||||
}
|
||||
|
||||
struct Reducer<'a> {
|
||||
symbol_table: &'a SymbolTable,
|
||||
}
|
||||
|
||||
impl<'a> Reducer<'a> {
|
||||
fn ast(&mut self, ast: &AST) -> ReducedAST {
|
||||
let mut output = vec![];
|
||||
for statement in ast.statements.iter() {
|
||||
output.push(self.statement(statement));
|
||||
}
|
||||
ReducedAST(output)
|
||||
}
|
||||
|
||||
fn statement(&mut self, stmt: &Statement) -> Stmt {
|
||||
match &stmt.kind {
|
||||
StatementKind::Expression(expr) => Stmt::Expr(self.expression(expr)),
|
||||
StatementKind::Declaration(decl) => self.declaration(decl),
|
||||
StatementKind::Import(_) => Stmt::Noop,
|
||||
StatementKind::Module(modspec) => {
|
||||
for statement in modspec.contents.iter() {
|
||||
self.statement(statement);
|
||||
}
|
||||
Stmt::Noop
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::ptr_arg)]
|
||||
fn block(&mut self, block: &Block) -> Vec<Stmt> {
|
||||
block.iter().map(|stmt| self.statement(stmt)).collect()
|
||||
}
|
||||
|
||||
fn invocation_argument(&mut self, invoc: &InvocationArgument) -> Expr {
|
||||
use crate::ast::InvocationArgument::*;
|
||||
match invoc {
|
||||
Positional(ex) => self.expression(ex),
|
||||
Keyword { .. } => Expr::UnimplementedSigilValue,
|
||||
Ignored => Expr::UnimplementedSigilValue,
|
||||
}
|
||||
}
|
||||
|
||||
fn expression(&mut self, expr: &Expression) -> Expr {
|
||||
use crate::ast::ExpressionKind::*;
|
||||
let input = &expr.kind;
|
||||
match input {
|
||||
NatLiteral(n) => Expr::Lit(Lit::Nat(*n)),
|
||||
FloatLiteral(f) => Expr::Lit(Lit::Float(*f)),
|
||||
StringLiteral(s) => Expr::Lit(Lit::StringLit(s.clone())),
|
||||
BoolLiteral(b) => Expr::Lit(Lit::Bool(*b)),
|
||||
BinExp(binop, lhs, rhs) => self.binop(binop, lhs, rhs),
|
||||
PrefixExp(op, arg) => self.prefix(op, arg),
|
||||
Value(qualified_name) => self.value(qualified_name),
|
||||
Call { f, arguments } => self.reduce_call_expression(f, arguments),
|
||||
TupleLiteral(exprs) => Expr::Tuple(exprs.iter().map(|e| self.expression(e)).collect()),
|
||||
IfExpression {
|
||||
discriminator,
|
||||
body,
|
||||
} => self.reduce_if_expression(deref_optional_box(discriminator), body),
|
||||
Lambda { params, body, .. } => self.reduce_lambda(params, body),
|
||||
NamedStruct { name, fields } => self.reduce_named_struct(name, fields),
|
||||
Index { .. } => Expr::UnimplementedSigilValue,
|
||||
WhileExpression { .. } => Expr::UnimplementedSigilValue,
|
||||
ForExpression { .. } => Expr::UnimplementedSigilValue,
|
||||
ListLiteral { .. } => Expr::UnimplementedSigilValue,
|
||||
}
|
||||
}
|
||||
|
||||
fn value(&mut self, qualified_name: &QualifiedName) -> Expr {
|
||||
let Symbol {
|
||||
local_name, spec, ..
|
||||
} = match self.symbol_table.lookup_symbol(&qualified_name.id) {
|
||||
Some(s) => s,
|
||||
//TODO this causes several evaluation tests to fail, figure out what's going on here
|
||||
//None => return Expr::ReductionError(format!("Symbol {:?} not found", sym_name)),
|
||||
None => {
|
||||
let name = qualified_name.components.last().unwrap().clone();
|
||||
return Expr::Sym(name);
|
||||
}
|
||||
};
|
||||
|
||||
match spec {
|
||||
SymbolSpec::RecordConstructor { .. } => Expr::ReductionError(
|
||||
"AST reducer doesn't expect a RecordConstructor here".to_string(),
|
||||
),
|
||||
SymbolSpec::DataConstructor {
|
||||
index,
|
||||
arity,
|
||||
type_name,
|
||||
} => Expr::Constructor {
|
||||
type_name: type_name.clone(),
|
||||
name: local_name.clone(),
|
||||
tag: *index,
|
||||
arity: *arity,
|
||||
},
|
||||
SymbolSpec::Func(_) => Expr::Sym(local_name.clone()),
|
||||
SymbolSpec::Binding => Expr::Sym(local_name.clone()), //TODO not sure if this is right, probably needs to eventually be fqsn
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::ptr_arg)]
|
||||
fn reduce_lambda(&mut self, params: &[FormalParam], body: &Block) -> Expr {
|
||||
Expr::Func(Func::UserDefined {
|
||||
name: None,
|
||||
params: params.iter().map(|param| param.name.clone()).collect(),
|
||||
body: self.block(body),
|
||||
})
|
||||
}
|
||||
|
||||
fn reduce_named_struct(
|
||||
&mut self,
|
||||
name: &QualifiedName,
|
||||
fields: &[(Rc<String>, Expression)],
|
||||
) -> Expr {
|
||||
let symbol = match self.symbol_table.lookup_symbol(&name.id) {
|
||||
Some(fqsn) => fqsn,
|
||||
None => return Expr::ReductionError(format!("FQSN lookup for name {:?} failed", name)),
|
||||
};
|
||||
|
||||
let (type_name, index, members_from_table) = match &symbol.spec {
|
||||
SymbolSpec::RecordConstructor {
|
||||
members,
|
||||
type_name,
|
||||
index,
|
||||
} => (type_name.clone(), index, members),
|
||||
_ => return Expr::ReductionError("Not a record constructor".to_string()),
|
||||
};
|
||||
|
||||
let arity = members_from_table.len();
|
||||
|
||||
let mut args: Vec<(Rc<String>, Expr)> = fields
|
||||
.iter()
|
||||
.map(|(name, expr)| (name.clone(), self.expression(expr)))
|
||||
.collect();
|
||||
|
||||
args.as_mut_slice()
|
||||
.sort_unstable_by(|(name1, _), (name2, _)| name1.cmp(name2)); //arbitrary - sorting by alphabetical order
|
||||
|
||||
let args = args.into_iter().map(|(_, expr)| expr).collect();
|
||||
|
||||
//TODO make sure this sorting actually works
|
||||
let f = box Expr::Constructor {
|
||||
type_name,
|
||||
name: symbol.local_name.clone(),
|
||||
tag: *index,
|
||||
arity,
|
||||
};
|
||||
Expr::Call { f, args }
|
||||
}
|
||||
|
||||
fn reduce_call_expression(
|
||||
&mut self,
|
||||
func: &Expression,
|
||||
arguments: &[InvocationArgument],
|
||||
) -> Expr {
|
||||
Expr::Call {
|
||||
f: Box::new(self.expression(func)),
|
||||
args: arguments
|
||||
.iter()
|
||||
.map(|arg| self.invocation_argument(arg))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn reduce_if_expression(
|
||||
&mut self,
|
||||
discriminator: Option<&Expression>,
|
||||
body: &IfExpressionBody,
|
||||
) -> Expr {
|
||||
let cond = Box::new(match discriminator {
|
||||
Some(expr) => self.expression(expr),
|
||||
None => return Expr::ReductionError("blank cond if-expr not supported".to_string()),
|
||||
});
|
||||
|
||||
match body {
|
||||
IfExpressionBody::SimpleConditional {
|
||||
then_case,
|
||||
else_case,
|
||||
} => {
|
||||
let then_clause = self.block(then_case);
|
||||
let else_clause = match else_case.as_ref() {
|
||||
None => vec![],
|
||||
Some(stmts) => self.block(stmts),
|
||||
};
|
||||
Expr::Conditional {
|
||||
cond,
|
||||
then_clause,
|
||||
else_clause,
|
||||
}
|
||||
}
|
||||
IfExpressionBody::SimplePatternMatch {
|
||||
pattern,
|
||||
then_case,
|
||||
else_case,
|
||||
} => {
|
||||
let then_clause = self.block(then_case);
|
||||
let else_clause = match else_case.as_ref() {
|
||||
None => vec![],
|
||||
Some(stmts) => self.block(stmts),
|
||||
};
|
||||
|
||||
let alternatives = vec![
|
||||
pattern.to_alternative(then_clause, self.symbol_table),
|
||||
Alternative {
|
||||
matchable: Subpattern {
|
||||
tag: None,
|
||||
subpatterns: vec![],
|
||||
bound_vars: vec![],
|
||||
guard: None,
|
||||
},
|
||||
item: else_clause,
|
||||
},
|
||||
];
|
||||
|
||||
Expr::CaseMatch { cond, alternatives }
|
||||
}
|
||||
IfExpressionBody::CondList(ref condition_arms) => {
|
||||
let mut alternatives = vec![];
|
||||
for arm in condition_arms {
|
||||
match arm.condition {
|
||||
Condition::Expression(ref _expr) => return Expr::UnimplementedSigilValue,
|
||||
Condition::Pattern(ref p) => {
|
||||
let item = self.block(&arm.body);
|
||||
let alt = p.to_alternative(item, self.symbol_table);
|
||||
alternatives.push(alt);
|
||||
}
|
||||
Condition::TruncatedOp(_, _) => return Expr::UnimplementedSigilValue,
|
||||
Condition::Else => return Expr::UnimplementedSigilValue,
|
||||
}
|
||||
}
|
||||
Expr::CaseMatch { cond, alternatives }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn binop(&mut self, binop: &BinOp, lhs: &Expression, rhs: &Expression) -> Expr {
|
||||
let operation = Builtin::from_str(binop.sigil()).ok();
|
||||
match operation {
|
||||
Some(Builtin::Assignment) => Expr::Assign {
|
||||
val: Box::new(self.expression(&*lhs)),
|
||||
expr: Box::new(self.expression(&*rhs)),
|
||||
},
|
||||
Some(op) => {
|
||||
let f = Box::new(Expr::Func(Func::BuiltIn(op)));
|
||||
Expr::Call {
|
||||
f,
|
||||
args: vec![self.expression(&*lhs), self.expression(&*rhs)],
|
||||
}
|
||||
}
|
||||
None => {
|
||||
//TODO handle a user-defined operation
|
||||
Expr::UnimplementedSigilValue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn prefix(&mut self, prefix: &PrefixOp, arg: &Expression) -> Expr {
|
||||
let builtin: Option<Builtin> = TryFrom::try_from(prefix).ok();
|
||||
match builtin {
|
||||
Some(op) => {
|
||||
let f = Box::new(Expr::Func(Func::BuiltIn(op)));
|
||||
Expr::Call {
|
||||
f,
|
||||
args: vec![self.expression(arg)],
|
||||
}
|
||||
}
|
||||
None => {
|
||||
//TODO need this for custom prefix ops
|
||||
Expr::UnimplementedSigilValue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn declaration(&mut self, declaration: &Declaration) -> Stmt {
|
||||
use self::Declaration::*;
|
||||
match declaration {
|
||||
Binding {
|
||||
name,
|
||||
constant,
|
||||
expr,
|
||||
..
|
||||
} => Stmt::Binding {
|
||||
name: name.clone(),
|
||||
constant: *constant,
|
||||
expr: self.expression(expr),
|
||||
},
|
||||
FuncDecl(Signature { name, params, .. }, statements) => Stmt::PreBinding {
|
||||
name: name.clone(),
|
||||
func: Func::UserDefined {
|
||||
name: Some(name.clone()),
|
||||
params: params.iter().map(|param| param.name.clone()).collect(),
|
||||
body: self.block(statements),
|
||||
},
|
||||
},
|
||||
TypeDecl { .. } => Stmt::Noop,
|
||||
TypeAlias { .. } => Stmt::Noop,
|
||||
Interface { .. } => Stmt::Noop,
|
||||
Impl { .. } => Stmt::Expr(Expr::UnimplementedSigilValue),
|
||||
Annotation { .. } => Stmt::Noop,
|
||||
_ => Stmt::Expr(Expr::UnimplementedSigilValue),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_symbol(
|
||||
symbol: Option<&Symbol>,
|
||||
inner_patterns: &[Pattern],
|
||||
symbol_table: &SymbolTable,
|
||||
) -> Subpattern {
|
||||
use self::Pattern::*;
|
||||
let tag = symbol.map(|symbol| match symbol.spec {
|
||||
SymbolSpec::DataConstructor { index, .. } => index,
|
||||
_ => {
|
||||
panic!("Symbol is not a data constructor - this should've been caught in type-checking")
|
||||
}
|
||||
});
|
||||
let bound_vars = inner_patterns
|
||||
.iter()
|
||||
.map(|p| match p {
|
||||
VarOrName(qualified_name) => {
|
||||
let symbol_exists = symbol_table.lookup_symbol(&qualified_name.id).is_some();
|
||||
if symbol_exists {
|
||||
None
|
||||
} else {
|
||||
let QualifiedName { components, .. } = qualified_name;
|
||||
if components.len() == 1 {
|
||||
Some(components[0].clone())
|
||||
} else {
|
||||
panic!("Bad variable name in pattern");
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
|
||||
let subpatterns = inner_patterns
|
||||
.iter()
|
||||
.map(|p| match p {
|
||||
Ignored => None,
|
||||
VarOrName(_) => None,
|
||||
Literal(other) => Some(other.to_subpattern(symbol_table)),
|
||||
tp @ TuplePattern(_) => Some(tp.to_subpattern(symbol_table)),
|
||||
ts @ TupleStruct(_, _) => Some(ts.to_subpattern(symbol_table)),
|
||||
Record(..) => unimplemented!(),
|
||||
})
|
||||
.collect();
|
||||
|
||||
let guard = None;
|
||||
/*
|
||||
let guard_equality_exprs: Vec<Expr> = subpatterns.iter().map(|p| match p {
|
||||
Literal(lit) => match lit {
|
||||
_ => unimplemented!()
|
||||
},
|
||||
_ => unimplemented!()
|
||||
}).collect();
|
||||
*/
|
||||
|
||||
Subpattern {
|
||||
tag,
|
||||
subpatterns,
|
||||
guard,
|
||||
bound_vars,
|
||||
}
|
||||
}
|
||||
|
||||
impl Pattern {
|
||||
fn to_alternative(&self, item: Vec<Stmt>, symbol_table: &SymbolTable) -> Alternative {
|
||||
let s = self.to_subpattern(symbol_table);
|
||||
Alternative {
|
||||
matchable: Subpattern {
|
||||
tag: s.tag,
|
||||
subpatterns: s.subpatterns,
|
||||
bound_vars: s.bound_vars,
|
||||
guard: s.guard,
|
||||
},
|
||||
item,
|
||||
}
|
||||
}
|
||||
|
||||
fn to_subpattern(&self, symbol_table: &SymbolTable) -> Subpattern {
|
||||
use self::Pattern::*;
|
||||
match self {
|
||||
TupleStruct(QualifiedName { components, id }, inner_patterns) => {
|
||||
match symbol_table.lookup_symbol(id) {
|
||||
Some(symbol) => handle_symbol(Some(symbol), inner_patterns, symbol_table),
|
||||
None => panic!("Symbol {:?} not found", components),
|
||||
}
|
||||
}
|
||||
TuplePattern(inner_patterns) => handle_symbol(None, inner_patterns, symbol_table),
|
||||
Record(_name, _pairs) => {
|
||||
unimplemented!()
|
||||
}
|
||||
Ignored => Subpattern {
|
||||
tag: None,
|
||||
subpatterns: vec![],
|
||||
guard: None,
|
||||
bound_vars: vec![],
|
||||
},
|
||||
Literal(lit) => lit.to_subpattern(symbol_table),
|
||||
VarOrName(QualifiedName { components, id }) => {
|
||||
// if symbol is Some, treat this as a symbol pattern. If it's None, treat it
|
||||
// as a variable.
|
||||
match symbol_table.lookup_symbol(id) {
|
||||
Some(symbol) => handle_symbol(Some(symbol), &[], symbol_table),
|
||||
None => {
|
||||
println!("Components: {:?}", components);
|
||||
let name = if components.len() == 1 {
|
||||
components[0].clone()
|
||||
} else {
|
||||
panic!("check this line of code yo");
|
||||
};
|
||||
Subpattern {
|
||||
tag: None,
|
||||
subpatterns: vec![],
|
||||
guard: None,
|
||||
bound_vars: vec![Some(name)],
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PatternLiteral {
|
||||
fn to_subpattern(&self, _symbol_table: &SymbolTable) -> Subpattern {
|
||||
use self::PatternLiteral::*;
|
||||
match self {
|
||||
NumPattern { neg, num } => {
|
||||
let comparison = Expr::Lit(match (neg, num) {
|
||||
(false, ExpressionKind::NatLiteral(n)) => Lit::Nat(*n),
|
||||
(false, ExpressionKind::FloatLiteral(f)) => Lit::Float(*f),
|
||||
(true, ExpressionKind::NatLiteral(n)) => Lit::Int(-(*n as i64)),
|
||||
(true, ExpressionKind::FloatLiteral(f)) => Lit::Float(-f),
|
||||
_ => panic!("This should never happen"),
|
||||
});
|
||||
let guard = Some(Expr::Call {
|
||||
f: Box::new(Expr::Func(Func::BuiltIn(Builtin::Equality))),
|
||||
args: vec![comparison, Expr::ConditionalTargetSigilValue],
|
||||
});
|
||||
Subpattern {
|
||||
tag: None,
|
||||
subpatterns: vec![],
|
||||
guard,
|
||||
bound_vars: vec![],
|
||||
}
|
||||
}
|
||||
StringPattern(s) => {
|
||||
let guard = Some(Expr::Call {
|
||||
f: Box::new(Expr::Func(Func::BuiltIn(Builtin::Equality))),
|
||||
args: vec![
|
||||
Expr::Lit(Lit::StringLit(s.clone())),
|
||||
Expr::ConditionalTargetSigilValue,
|
||||
],
|
||||
});
|
||||
|
||||
Subpattern {
|
||||
tag: None,
|
||||
subpatterns: vec![],
|
||||
guard,
|
||||
bound_vars: vec![],
|
||||
}
|
||||
}
|
||||
BoolPattern(b) => {
|
||||
let guard = Some(if *b {
|
||||
Expr::ConditionalTargetSigilValue
|
||||
} else {
|
||||
Expr::Call {
|
||||
f: Box::new(Expr::Func(Func::BuiltIn(Builtin::BooleanNot))),
|
||||
args: vec![Expr::ConditionalTargetSigilValue],
|
||||
}
|
||||
});
|
||||
Subpattern {
|
||||
tag: None,
|
||||
subpatterns: vec![],
|
||||
guard,
|
||||
bound_vars: vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,98 +0,0 @@
|
||||
#![allow(clippy::enum_variant_names)]
|
||||
|
||||
use crate::builtin::Builtin;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ReducedAST(pub Vec<Stmt>);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Stmt {
|
||||
PreBinding {
|
||||
name: Rc<String>,
|
||||
func: Func,
|
||||
},
|
||||
Binding {
|
||||
name: Rc<String>,
|
||||
constant: bool,
|
||||
expr: Expr,
|
||||
},
|
||||
Expr(Expr),
|
||||
Noop,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Expr {
|
||||
Lit(Lit),
|
||||
Sym(Rc<String>), //a Sym is anything that can be looked up by name at runtime - i.e. a function or variable address
|
||||
Tuple(Vec<Expr>),
|
||||
Func(Func),
|
||||
Constructor {
|
||||
type_name: Rc<String>,
|
||||
name: Rc<String>,
|
||||
tag: usize,
|
||||
arity: usize, // n.b. arity here is always the value from the symbol table - if it doesn't match what it's being called with, that's an eval error, eval will handle it
|
||||
},
|
||||
Call {
|
||||
f: Box<Expr>,
|
||||
args: Vec<Expr>,
|
||||
},
|
||||
Assign {
|
||||
val: Box<Expr>, //TODO this probably can't be a val
|
||||
expr: Box<Expr>,
|
||||
},
|
||||
Conditional {
|
||||
cond: Box<Expr>,
|
||||
then_clause: Vec<Stmt>,
|
||||
else_clause: Vec<Stmt>,
|
||||
},
|
||||
ConditionalTargetSigilValue,
|
||||
CaseMatch {
|
||||
cond: Box<Expr>,
|
||||
alternatives: Vec<Alternative>,
|
||||
},
|
||||
UnimplementedSigilValue,
|
||||
ReductionError(String),
|
||||
}
|
||||
|
||||
impl Expr {
|
||||
// The unit value is an empty tuple
|
||||
pub fn unit() -> Expr {
|
||||
Expr::Tuple(vec![])
|
||||
}
|
||||
}
|
||||
|
||||
pub type BoundVars = Vec<Option<Rc<String>>>; //remember that order matters here
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Alternative {
|
||||
pub matchable: Subpattern,
|
||||
pub item: Vec<Stmt>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Subpattern {
|
||||
pub tag: Option<usize>,
|
||||
pub subpatterns: Vec<Option<Subpattern>>,
|
||||
pub bound_vars: BoundVars,
|
||||
pub guard: Option<Expr>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Lit {
|
||||
Nat(u64),
|
||||
Int(i64),
|
||||
Float(f64),
|
||||
Bool(bool),
|
||||
StringLit(Rc<String>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Func {
|
||||
BuiltIn(Builtin),
|
||||
UserDefined {
|
||||
name: Option<Rc<String>>,
|
||||
params: Vec<Rc<String>>,
|
||||
body: Vec<Stmt>,
|
||||
},
|
||||
}
|
557
schala-lang/language/src/reduced_ir/mod.rs
Normal file
557
schala-lang/language/src/reduced_ir/mod.rs
Normal file
@ -0,0 +1,557 @@
|
||||
use crate::ast;
|
||||
use crate::symbol_table::{DefId, SymbolSpec, SymbolTable};
|
||||
use crate::builtin::Builtin;
|
||||
|
||||
use std::str::FromStr;
|
||||
use std::collections::HashMap;
|
||||
|
||||
mod types;
|
||||
mod test;
|
||||
|
||||
pub use types::*;
|
||||
|
||||
pub fn reduce(ast: &ast::AST, symbol_table: &SymbolTable) -> ReducedIR {
|
||||
let reducer = Reducer::new(symbol_table);
|
||||
reducer.reduce(ast)
|
||||
}
|
||||
|
||||
struct Reducer<'a> {
|
||||
symbol_table: &'a SymbolTable,
|
||||
functions: HashMap<DefId, FunctionDefinition>,
|
||||
}
|
||||
|
||||
impl<'a> Reducer<'a> {
|
||||
fn new(symbol_table: &'a SymbolTable) -> Self {
|
||||
Self {
|
||||
symbol_table,
|
||||
functions: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn reduce(mut self, ast: &ast::AST) -> ReducedIR {
|
||||
// First reduce all functions
|
||||
// TODO once this works, maybe rewrite it using the Visitor
|
||||
for statement in ast.statements.iter() {
|
||||
self.top_level_statement(&statement);
|
||||
}
|
||||
|
||||
// Then compute the entrypoint statements (which may reference previously-computed
|
||||
// functions by ID)
|
||||
let mut entrypoint = vec![];
|
||||
for statement in ast.statements.iter() {
|
||||
let ast::Statement { id: item_id, kind, .. } = statement;
|
||||
match &kind {
|
||||
ast::StatementKind::Expression(expr) => {
|
||||
entrypoint.push(Statement::Expression(self.expression(&expr)));
|
||||
},
|
||||
ast::StatementKind::Declaration(ast::Declaration::Binding { name: _, constant, expr, ..}) => {
|
||||
let symbol = self.symbol_table.lookup_symbol(item_id).unwrap();
|
||||
let def_id = symbol.def_id().unwrap();
|
||||
entrypoint.push(Statement::Binding { id: def_id, constant: *constant, expr: self.expression(&expr) });
|
||||
},
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
|
||||
ReducedIR {
|
||||
functions: self.functions,
|
||||
entrypoint,
|
||||
}
|
||||
}
|
||||
|
||||
fn top_level_statement(&mut self, statement: &ast::Statement) {
|
||||
let ast::Statement { id: item_id, kind, .. } = statement;
|
||||
match kind {
|
||||
ast::StatementKind::Expression(_expr) => {
|
||||
//TODO expressions can in principle contain definitions, but I won't worry
|
||||
//about it now
|
||||
()
|
||||
},
|
||||
ast::StatementKind::Declaration(decl) => match decl {
|
||||
ast::Declaration::FuncDecl(_, statements) => {
|
||||
self.insert_function_definition(item_id, statements);
|
||||
},
|
||||
_ => ()
|
||||
},
|
||||
ast::StatementKind::Import(..) => (),
|
||||
ast::StatementKind::Module(_modspec) => {
|
||||
//TODO handle modules
|
||||
()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn function_internal_statement(&mut self, statement: &ast::Statement) -> Option<Statement> {
|
||||
let ast::Statement { id: item_id, kind, .. } = statement;
|
||||
match kind {
|
||||
ast::StatementKind::Expression(expr) => {
|
||||
Some(Statement::Expression(self.expression(expr)))
|
||||
},
|
||||
ast::StatementKind::Declaration(decl) => match decl {
|
||||
ast::Declaration::FuncDecl(_, statements) => {
|
||||
self.insert_function_definition(item_id, statements);
|
||||
None
|
||||
},
|
||||
ast::Declaration::Binding { constant, expr, ..} => {
|
||||
let symbol = self.symbol_table.lookup_symbol(item_id).unwrap();
|
||||
let def_id = symbol.def_id().unwrap();
|
||||
Some(Statement::Binding { id: def_id, constant: *constant, expr: self.expression(&expr) })
|
||||
},
|
||||
|
||||
_ => None
|
||||
},
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_function_definition(&mut self, item_id: &ast::ItemId, statements: &ast::Block) {
|
||||
let symbol = self.symbol_table.lookup_symbol(item_id).unwrap();
|
||||
let def_id = symbol.def_id().unwrap();
|
||||
let function_def = FunctionDefinition {
|
||||
body: self.function_internal_block(statements)
|
||||
};
|
||||
self.functions.insert(def_id, function_def);
|
||||
}
|
||||
|
||||
fn expression(&mut self, expr: &ast::Expression) -> Expression {
|
||||
use crate::ast::ExpressionKind::*;
|
||||
|
||||
match &expr.kind {
|
||||
NatLiteral(n) => Expression::Literal(Literal::Nat(*n)),
|
||||
FloatLiteral(f) => Expression::Literal(Literal::Float(*f)),
|
||||
StringLiteral(s) => Expression::Literal(Literal::StringLit(s.clone())),
|
||||
BoolLiteral(b) => Expression::Literal(Literal::Bool(*b)),
|
||||
BinExp(binop, lhs, rhs) => self.binop(binop, lhs, rhs),
|
||||
PrefixExp(op, arg) => self.prefix(op, arg),
|
||||
Value(qualified_name) => self.value(qualified_name),
|
||||
Call { f, arguments } => Expression::Call {
|
||||
f: Box::new(self.expression(f)),
|
||||
args: arguments
|
||||
.iter()
|
||||
.map(|arg| self.invocation_argument(arg))
|
||||
.collect(),
|
||||
},
|
||||
TupleLiteral(exprs) => Expression::Tuple(exprs.iter().map(|e| self.expression(e)).collect()),
|
||||
IfExpression { discriminator, body, } => self.reduce_if_expression(discriminator.as_ref().map(|x| x.as_ref()), body),
|
||||
Lambda { params, body, .. } => {
|
||||
Expression::Callable(Callable::Lambda {
|
||||
arity: params.len() as u8,
|
||||
body: self.function_internal_block(body),
|
||||
})
|
||||
},
|
||||
NamedStruct { .. } => Expression::ReductionError("NamedStruct not implemented".to_string()), //self.reduce_named_struct(name, fields),
|
||||
Index { .. } => Expression::ReductionError("Index expr not implemented".to_string()),
|
||||
WhileExpression { .. } => Expression::ReductionError("While expr not implemented".to_string()),
|
||||
ForExpression { .. } => Expression::ReductionError("For expr not implemented".to_string()),
|
||||
ListLiteral { .. } => Expression::ReductionError("ListLiteral expr not implemented".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
fn reduce_if_expression(&mut self, discriminator: Option<&ast::Expression>, body: &ast::IfExpressionBody) -> Expression {
|
||||
use ast::IfExpressionBody::*;
|
||||
|
||||
let cond = Box::new(match discriminator {
|
||||
Some(expr) => self.expression(expr),
|
||||
None => return Expression::ReductionError("blank cond if-expr not supported".to_string()),
|
||||
});
|
||||
match body {
|
||||
SimpleConditional {
|
||||
then_case,
|
||||
else_case,
|
||||
} => {
|
||||
let then_clause = self.function_internal_block(then_case);
|
||||
let else_clause = match else_case.as_ref() {
|
||||
None => vec![],
|
||||
Some(stmts) => self.function_internal_block(stmts),
|
||||
};
|
||||
Expression::Conditional {
|
||||
cond,
|
||||
then_clause,
|
||||
else_clause,
|
||||
}
|
||||
},
|
||||
SimplePatternMatch {
|
||||
pattern,
|
||||
then_case,
|
||||
else_case,
|
||||
} => {
|
||||
let alternatives = vec![
|
||||
Alternative {
|
||||
pattern: match pattern.reduce(self.symbol_table) {
|
||||
Ok(p) => p,
|
||||
Err(e) => return Expression::ReductionError(format!("Bad pattern: {:?}", e)),
|
||||
},
|
||||
item: self.function_internal_block(then_case),
|
||||
},
|
||||
Alternative {
|
||||
pattern: Pattern::Ignored,
|
||||
item: match else_case.as_ref() {
|
||||
Some(else_case) => self.function_internal_block(else_case),
|
||||
None => vec![],
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
Expression::CaseMatch { cond, alternatives }
|
||||
},
|
||||
CondList(ref condition_arms) => {
|
||||
let mut alternatives = vec![];
|
||||
for arm in condition_arms {
|
||||
match arm.condition {
|
||||
ast::Condition::Expression(ref _expr) => return Expression::ReductionError("case-expression".to_string()),
|
||||
ast::Condition::Pattern(ref pat) => {
|
||||
let alt = Alternative {
|
||||
pattern: match pat.reduce(self.symbol_table) {
|
||||
Ok(p) => p,
|
||||
Err(e) => return Expression::ReductionError(format!("Bad pattern: {:?}", e)),
|
||||
},
|
||||
item: self.function_internal_block(&arm.body),
|
||||
};
|
||||
alternatives.push(alt);
|
||||
},
|
||||
ast::Condition::TruncatedOp(_, _) => return Expression::ReductionError("case-expression-trunc-op".to_string()),
|
||||
ast::Condition::Else => return Expression::ReductionError("case-expression-else".to_string()),
|
||||
}
|
||||
}
|
||||
Expression::CaseMatch { cond, alternatives }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn invocation_argument(&mut self, invoc: &ast::InvocationArgument) -> Expression {
|
||||
use crate::ast::InvocationArgument::*;
|
||||
match invoc {
|
||||
Positional(ex) => self.expression(ex),
|
||||
Keyword { .. } => Expression::ReductionError("Keyword arguments not supported".to_string()),
|
||||
Ignored => Expression::ReductionError("Ignored arguments not supported".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
fn function_internal_block(&mut self, statements: &ast::Block) -> Vec<Statement> {
|
||||
statements.iter().filter_map(|stmt| self.function_internal_statement(stmt)).collect()
|
||||
}
|
||||
|
||||
fn prefix(&mut self, prefix: &ast::PrefixOp, arg: &ast::Expression) -> Expression {
|
||||
let builtin: Option<Builtin> = TryFrom::try_from(prefix).ok();
|
||||
match builtin {
|
||||
Some(op) => {
|
||||
Expression::Call {
|
||||
f: Box::new(Expression::Callable(Callable::Builtin(op))),
|
||||
args: vec![self.expression(arg)],
|
||||
}
|
||||
}
|
||||
None => {
|
||||
//TODO need this for custom prefix ops
|
||||
Expression::ReductionError("User-defined prefix ops not supported".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn binop(&mut self, binop: &ast::BinOp, lhs: &ast::Expression, rhs: &ast::Expression) -> Expression {
|
||||
use Expression::ReductionError;
|
||||
|
||||
let operation = Builtin::from_str(binop.sigil()).ok();
|
||||
match operation {
|
||||
Some(Builtin::Assignment) => {
|
||||
let lval = match &lhs.kind {
|
||||
ast::ExpressionKind::Value(qualified_name) => {
|
||||
if let Some(symbol) = self.symbol_table.lookup_symbol(&qualified_name.id) {
|
||||
symbol.def_id().unwrap()
|
||||
} else {
|
||||
return ReductionError(format!("Couldn't look up name: {:?}", qualified_name));
|
||||
}
|
||||
},
|
||||
_ => return ReductionError("Trying to assign to a non-name".to_string()),
|
||||
};
|
||||
|
||||
Expression::Assign {
|
||||
lval,
|
||||
rval: Box::new(self.expression(rhs)),
|
||||
}
|
||||
},
|
||||
Some(op) => Expression::Call {
|
||||
f: Box::new(Expression::Callable(Callable::Builtin(op))),
|
||||
args: vec![self.expression(lhs), self.expression(rhs)],
|
||||
},
|
||||
//TODO handle a user-defined operation
|
||||
None => ReductionError("User-defined operations not supported".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn value(&mut self, qualified_name: &ast::QualifiedName) -> Expression {
|
||||
use SymbolSpec::*;
|
||||
|
||||
let symbol = match self.symbol_table.lookup_symbol(&qualified_name.id) {
|
||||
Some(s) => s,
|
||||
None => return Expression::ReductionError(format!("No symbol found for name: {:?}", qualified_name))
|
||||
};
|
||||
|
||||
let def_id = symbol.def_id();
|
||||
|
||||
match symbol.spec() {
|
||||
Func => Expression::Lookup(Lookup::Function(def_id.unwrap())),
|
||||
GlobalBinding => Expression::Lookup(Lookup::GlobalVar(def_id.unwrap())),
|
||||
LocalVariable => Expression::Lookup(Lookup::LocalVar(def_id.unwrap())),
|
||||
FunctionParam(n) => Expression::Lookup(Lookup::Param(n)),
|
||||
DataConstructor { index, arity, type_id } => Expression::Callable(Callable::DataConstructor {
|
||||
type_id: type_id.clone(),
|
||||
arity: arity as u32, //TODO fix up these modifiers
|
||||
tag: index as u32,
|
||||
}),
|
||||
RecordConstructor { .. } => {
|
||||
Expression::ReductionError(format!("The symbol for value {:?} is unexpectdly a RecordConstructor", qualified_name))
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::Pattern {
|
||||
fn reduce(&self, symbol_table: &SymbolTable) -> Result<Pattern, PatternError> {
|
||||
Ok(match self {
|
||||
ast::Pattern::Ignored => Pattern::Ignored,
|
||||
ast::Pattern::TuplePattern(subpatterns) => {
|
||||
let items: Vec<_> = subpatterns.iter().map(|pat| pat.reduce(symbol_table)).collect();
|
||||
let items: Result<Vec<Pattern>, PatternError> = items.into_iter().collect();
|
||||
let items = items?;
|
||||
Pattern::Tuple {
|
||||
tag: None,
|
||||
subpatterns: items,
|
||||
}
|
||||
},
|
||||
ast::Pattern::Literal(lit) => Pattern::Literal(match lit {
|
||||
ast::PatternLiteral::NumPattern { neg, num } => match (neg, num) {
|
||||
(false, ast::ExpressionKind::NatLiteral(n)) => Literal::Nat(*n),
|
||||
(false, ast::ExpressionKind::FloatLiteral(f)) => Literal::Float(*f),
|
||||
(true, ast::ExpressionKind::NatLiteral(n)) => Literal::Int(-(*n as i64)),
|
||||
(true, ast::ExpressionKind::FloatLiteral(f)) => Literal::Float(-f),
|
||||
(_, e) => return Err(format!("Internal error, unexpected pattern literal: {:?}", e).into())
|
||||
},
|
||||
ast::PatternLiteral::StringPattern(s) => Literal::StringLit(s.clone()),
|
||||
ast::PatternLiteral::BoolPattern(b) => Literal::Bool(*b),
|
||||
}),
|
||||
ast::Pattern::TupleStruct(name, subpatterns) => {
|
||||
let symbol = symbol_table.lookup_symbol(&name.id).unwrap();
|
||||
if let SymbolSpec::DataConstructor { index: tag, type_id, arity } = symbol.spec() {
|
||||
let items: Vec<_> = subpatterns.iter().map(|pat| pat.reduce(symbol_table)).collect();
|
||||
let items: Result<Vec<Pattern>, PatternError> = items.into_iter().collect();
|
||||
let items = items?;
|
||||
Pattern::Tuple {
|
||||
tag: Some(tag as u32),
|
||||
subpatterns: items,
|
||||
}
|
||||
} else {
|
||||
return Err("Internal error, trying to match something that's not a DataConstructor".into());
|
||||
}
|
||||
},
|
||||
ast::Pattern::VarOrName(name) => {
|
||||
let symbol = symbol_table.lookup_symbol(&name.id).unwrap();
|
||||
match symbol.spec() {
|
||||
SymbolSpec::DataConstructor { index: tag, type_id, arity } => {
|
||||
Pattern::Tuple {
|
||||
tag: Some(tag as u32),
|
||||
subpatterns: vec![]
|
||||
}
|
||||
},
|
||||
SymbolSpec::LocalVariable => {
|
||||
let def_id = symbol.def_id().unwrap().clone();
|
||||
Pattern::Binding(def_id)
|
||||
},
|
||||
spec => return Err(format!("Unexpected VarOrName symbol: {:?}", spec).into())
|
||||
}
|
||||
},
|
||||
ast::Pattern::Record(name, /*Vec<(Rc<String>, Pattern)>*/ _) => {
|
||||
unimplemented!()
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
impl ast::Pattern {
|
||||
fn to_subpattern(&self, symbol_table: &SymbolTable) -> Subpattern {
|
||||
use ast::Pattern::*;
|
||||
use Expression::ReductionError;
|
||||
|
||||
match self {
|
||||
Ignored => Subpattern {
|
||||
tag: None,
|
||||
subpatterns: vec![],
|
||||
guard: None,
|
||||
},
|
||||
Literal(lit) => lit.to_subpattern(symbol_table),
|
||||
/*
|
||||
TupleStruct(QualifiedName { components, id }, inner_patterns) => {
|
||||
match symbol_table.lookup_symbol(id) {
|
||||
Some(symbol) => handle_symbol(Some(symbol), inner_patterns, symbol_table),
|
||||
None => panic!("Symbol {:?} not found", components),
|
||||
}
|
||||
}
|
||||
*/
|
||||
_ => Subpattern {
|
||||
tag: None,
|
||||
subpatterns: vec![],
|
||||
guard: None,
|
||||
}
|
||||
}
|
||||
/*
|
||||
TuplePattern(inner_patterns) => handle_symbol(None, inner_patterns, symbol_table),
|
||||
Record(_name, _pairs) => {
|
||||
unimplemented!()
|
||||
}
|
||||
VarOrName(QualifiedName { components, id }) => {
|
||||
// if symbol is Some, treat this as a symbol pattern. If it's None, treat it
|
||||
// as a variable.
|
||||
match symbol_table.lookup_symbol(id) {
|
||||
Some(symbol) => handle_symbol(Some(symbol), &[], symbol_table),
|
||||
None => {
|
||||
println!("Components: {:?}", components);
|
||||
let name = if components.len() == 1 {
|
||||
components[0].clone()
|
||||
} else {
|
||||
panic!("check this line of code yo");
|
||||
};
|
||||
Subpattern {
|
||||
tag: None,
|
||||
subpatterns: vec![],
|
||||
guard: None,
|
||||
bound_vars: vec![Some(name)],
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
fn handle_symbol(
|
||||
symbol: Option<&Symbol>,
|
||||
inner_patterns: &[Pattern],
|
||||
symbol_table: &SymbolTable,
|
||||
) -> Subpattern {
|
||||
use ast::Pattern::*;
|
||||
|
||||
let tag = symbol.map(|symbol| match symbol.spec {
|
||||
SymbolSpec::DataConstructor { index, .. } => index,
|
||||
_ => {
|
||||
panic!("Symbol is not a data constructor - this should've been caught in type-checking")
|
||||
}
|
||||
});
|
||||
let bound_vars = inner_patterns
|
||||
.iter()
|
||||
.map(|p| match p {
|
||||
VarOrName(qualified_name) => {
|
||||
let symbol_exists = symbol_table.lookup_symbol(&qualified_name.id).is_some();
|
||||
if symbol_exists {
|
||||
None
|
||||
} else {
|
||||
let QualifiedName { components, .. } = qualified_name;
|
||||
if components.len() == 1 {
|
||||
Some(components[0].clone())
|
||||
} else {
|
||||
panic!("Bad variable name in pattern");
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
|
||||
let subpatterns = inner_patterns
|
||||
.iter()
|
||||
.map(|p| match p {
|
||||
Ignored => None,
|
||||
VarOrName(_) => None,
|
||||
Literal(other) => Some(other.to_subpattern(symbol_table)),
|
||||
tp @ TuplePattern(_) => Some(tp.to_subpattern(symbol_table)),
|
||||
ts @ TupleStruct(_, _) => Some(ts.to_subpattern(symbol_table)),
|
||||
Record(..) => unimplemented!(),
|
||||
})
|
||||
.collect();
|
||||
|
||||
let guard = None;
|
||||
/*
|
||||
let guard_equality_exprs: Vec<Expr> = subpatterns.iter().map(|p| match p {
|
||||
Literal(lit) => match lit {
|
||||
_ => unimplemented!()
|
||||
},
|
||||
_ => unimplemented!()
|
||||
}).collect();
|
||||
*/
|
||||
|
||||
Subpattern {
|
||||
tag,
|
||||
subpatterns,
|
||||
guard,
|
||||
bound_vars,
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
impl ast::PatternLiteral {
|
||||
fn to_subpattern(&self, _symbol_table: &SymbolTable) -> Subpattern {
|
||||
use ast::PatternLiteral::*;
|
||||
|
||||
match self {
|
||||
NumPattern { neg, num } => {
|
||||
let comparison = Expression::Literal(match (neg, num) {
|
||||
(false, ast::ExpressionKind::NatLiteral(n)) => Literal::Nat(*n),
|
||||
(false, ast::ExpressionKind::FloatLiteral(f)) => Literal::Float(*f),
|
||||
(true, ast::ExpressionKind::NatLiteral(n)) => Literal::Int(-(*n as i64)),
|
||||
(true, ast::ExpressionKind::FloatLiteral(f)) => Literal::Float(-f),
|
||||
_ => panic!("This should never happen"),
|
||||
});
|
||||
unimplemented!()
|
||||
/*
|
||||
let guard = Some(Expr::Call {
|
||||
f: Box::new(Expr::Func(Func::BuiltIn(Builtin::Equality))),
|
||||
args: vec![comparison, Expr::ConditionalTargetSigilValue],
|
||||
});
|
||||
Subpattern {
|
||||
tag: None,
|
||||
subpatterns: vec![],
|
||||
guard,
|
||||
}
|
||||
*/
|
||||
}
|
||||
_ => unimplemented!()
|
||||
/*
|
||||
StringPattern(s) => {
|
||||
let guard = Some(Expr::Call {
|
||||
f: Box::new(Expr::Func(Func::BuiltIn(Builtin::Equality))),
|
||||
args: vec![
|
||||
Expr::Lit(Lit::StringLit(s.clone())),
|
||||
Expr::ConditionalTargetSigilValue,
|
||||
],
|
||||
});
|
||||
|
||||
Subpattern {
|
||||
tag: None,
|
||||
subpatterns: vec![],
|
||||
guard,
|
||||
bound_vars: vec![],
|
||||
}
|
||||
}
|
||||
BoolPattern(b) => {
|
||||
let guard = Some(if *b {
|
||||
Expr::ConditionalTargetSigilValue
|
||||
} else {
|
||||
Expr::Call {
|
||||
f: Box::new(Expr::Func(Func::BuiltIn(Builtin::BooleanNot))),
|
||||
args: vec![Expr::ConditionalTargetSigilValue],
|
||||
}
|
||||
});
|
||||
Subpattern {
|
||||
tag: None,
|
||||
subpatterns: vec![],
|
||||
guard,
|
||||
bound_vars: vec![],
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
43
schala-lang/language/src/reduced_ir/test.rs
Normal file
43
schala-lang/language/src/reduced_ir/test.rs
Normal file
@ -0,0 +1,43 @@
|
||||
#![cfg(test)]
|
||||
|
||||
use crate::symbol_table::SymbolTable;
|
||||
use super::*;
|
||||
|
||||
fn build_ir(input: &str) -> ReducedIR {
|
||||
let ast = crate::util::quick_ast(input);
|
||||
|
||||
let mut symbol_table = SymbolTable::new();
|
||||
symbol_table.process_ast(&ast).unwrap();
|
||||
|
||||
let reduced = reduce(&ast, &symbol_table);
|
||||
reduced.debug(&symbol_table);
|
||||
reduced
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_ir() {
|
||||
let src = r#"
|
||||
|
||||
let global_one = 10 + 20
|
||||
let global_two = "the string hello"
|
||||
|
||||
fn a_function(i, j, k) {
|
||||
fn nested(x) {
|
||||
x + 10
|
||||
}
|
||||
i + j * nested(k)
|
||||
}
|
||||
|
||||
fn another_function(e) {
|
||||
let local_var = 420
|
||||
e * local_var
|
||||
}
|
||||
|
||||
another_function()
|
||||
"#;
|
||||
|
||||
let reduced = build_ir(src);
|
||||
assert_eq!(reduced.functions.len(), 3);
|
||||
//assert!(1 == 2);
|
||||
}
|
162
schala-lang/language/src/reduced_ir/types.rs
Normal file
162
schala-lang/language/src/reduced_ir/types.rs
Normal file
@ -0,0 +1,162 @@
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
use std::convert::From;
|
||||
|
||||
use crate::builtin::Builtin;
|
||||
use crate::symbol_table::{DefId, SymbolTable};
|
||||
use crate::typechecking::TypeId;
|
||||
|
||||
//TODO most of these Clone impls only exist to support function application, because the
|
||||
//tree-walking evaluator moves the reduced IR members.
|
||||
|
||||
/// The reduced intermediate representation consists of a list of function definitions, and a block
|
||||
/// of entrypoint statements. In a repl or script context this can be an arbitrary list of
|
||||
/// statements, in an executable context will likely just be a pointer to the main() function.
|
||||
#[derive(Debug)]
|
||||
pub struct ReducedIR {
|
||||
pub functions: HashMap<DefId, FunctionDefinition>,
|
||||
pub entrypoint: Vec<Statement>,
|
||||
}
|
||||
|
||||
impl ReducedIR {
|
||||
#[allow(dead_code)]
|
||||
pub fn debug(&self, symbol_table: &SymbolTable) {
|
||||
println!("Reduced IR:");
|
||||
println!("Functions:");
|
||||
println!("-----------");
|
||||
for (id, callable) in self.functions.iter() {
|
||||
let name = &symbol_table.lookup_symbol_by_def(id).unwrap().local_name();
|
||||
println!("{}({}) -> {:?}", id, name, callable);
|
||||
}
|
||||
println!("");
|
||||
println!("Entrypoint:");
|
||||
println!("-----------");
|
||||
for stmt in self.entrypoint.iter() {
|
||||
println!("{:?}", stmt);
|
||||
}
|
||||
println!("-----------");
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Statement {
|
||||
Expression(Expression),
|
||||
Binding {
|
||||
id: DefId,
|
||||
constant: bool,
|
||||
expr: Expression
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Expression {
|
||||
Literal(Literal),
|
||||
Tuple(Vec<Expression>),
|
||||
Lookup(Lookup),
|
||||
Assign {
|
||||
lval: DefId,
|
||||
rval: Box<Expression>,
|
||||
},
|
||||
Callable(Callable),
|
||||
Call {
|
||||
f: Box<Expression>,
|
||||
args: Vec<Expression>
|
||||
},
|
||||
Conditional {
|
||||
cond: Box<Expression>,
|
||||
then_clause: Vec<Statement>,
|
||||
else_clause: Vec<Statement>,
|
||||
},
|
||||
CaseMatch {
|
||||
cond: Box<Expression>,
|
||||
alternatives: Vec<Alternative>,
|
||||
},
|
||||
ReductionError(String),
|
||||
}
|
||||
|
||||
impl Expression {
|
||||
pub fn unit() -> Self {
|
||||
Expression::Tuple(vec![])
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FunctionDefinition {
|
||||
pub body: Vec<Statement>
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Callable {
|
||||
Builtin(Builtin),
|
||||
UserDefined(DefId),
|
||||
Lambda {
|
||||
arity: u8,
|
||||
body: Vec<Statement>
|
||||
},
|
||||
DataConstructor {
|
||||
type_id: TypeId,
|
||||
arity: u32,
|
||||
tag: u32
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Lookup {
|
||||
LocalVar(DefId),
|
||||
GlobalVar(DefId),
|
||||
Function(DefId),
|
||||
Param(u8),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Literal {
|
||||
Nat(u64),
|
||||
Int(i64),
|
||||
Float(f64),
|
||||
Bool(bool),
|
||||
StringLit(Rc<String>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Alternative {
|
||||
pub pattern: Pattern,
|
||||
pub item: Vec<Statement>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Pattern {
|
||||
Tuple {
|
||||
subpatterns: Vec<Pattern>,
|
||||
tag: Option<u32>,
|
||||
},
|
||||
Literal(Literal),
|
||||
Ignored,
|
||||
Binding(DefId)
|
||||
}
|
||||
|
||||
/*
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Subpattern {
|
||||
pub tag: Option<usize>,
|
||||
pub subpatterns: Vec<Option<Subpattern>>,
|
||||
//pub bound_vars: BoundVars,
|
||||
pub guard: Option<Expression>,
|
||||
}
|
||||
*/
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PatternError {
|
||||
msg: String,
|
||||
}
|
||||
|
||||
impl From<&str> for PatternError {
|
||||
fn from(s: &str) -> Self {
|
||||
Self { msg: s.to_string() }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for PatternError {
|
||||
fn from(msg: String) -> Self {
|
||||
Self { msg }
|
||||
}
|
||||
}
|
@ -1,26 +1,31 @@
|
||||
use stopwatch::Stopwatch;
|
||||
|
||||
use crate::error::SchalaError;
|
||||
use crate::{eval, parsing, reduced_ast, symbol_table, tokenizing, typechecking};
|
||||
use crate::{/*eval, */parsing, reduced_ir, tree_walk_eval, symbol_table, tokenizing, typechecking};
|
||||
use schala_repl::{
|
||||
ComputationRequest, ComputationResponse, GlobalOutputStats, LangMetaRequest, LangMetaResponse,
|
||||
ProgrammingLanguageInterface,
|
||||
};
|
||||
|
||||
/// All the state necessary to parse and execute a Schala program are stored in this struct.
|
||||
pub struct Schala {
|
||||
pub struct Schala<'a> {
|
||||
/// Holds a reference to the original source code, parsed into line and character
|
||||
source_reference: SourceReference,
|
||||
/// Execution state for AST-walking interpreter
|
||||
state: eval::State<'static>,
|
||||
|
||||
//state: eval::State<'static>,
|
||||
/// Keeps track of symbols and scopes
|
||||
symbol_table: symbol_table::SymbolTable,
|
||||
/// Contains information for type-checking
|
||||
type_context: typechecking::TypeContext<'static>,
|
||||
/// Schala Parser
|
||||
active_parser: parsing::Parser,
|
||||
|
||||
/// Execution state for AST-walking interpreter
|
||||
eval_state: tree_walk_eval::State<'a>,
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
impl Schala {
|
||||
//TODO implement documentation for language items
|
||||
/*
|
||||
@ -31,23 +36,25 @@ impl Schala {
|
||||
}
|
||||
*/
|
||||
}
|
||||
*/
|
||||
|
||||
impl Schala {
|
||||
impl<'a> Schala<'a> {
|
||||
/// Creates a new Schala environment *without* any prelude.
|
||||
fn new_blank_env() -> Schala {
|
||||
fn new_blank_env() -> Schala<'a> {
|
||||
Schala {
|
||||
source_reference: SourceReference::new(),
|
||||
symbol_table: symbol_table::SymbolTable::new(),
|
||||
state: eval::State::new(),
|
||||
//state: eval::State::new(),
|
||||
type_context: typechecking::TypeContext::new(),
|
||||
active_parser: parsing::Parser::new(),
|
||||
eval_state: tree_walk_eval::State::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new Schala environment with the standard prelude, which is defined as ordinary
|
||||
/// Schala code in the file `prelude.schala`
|
||||
#[allow(clippy::new_without_default)]
|
||||
pub fn new() -> Schala {
|
||||
pub fn new() -> Schala<'a> {
|
||||
let prelude = include_str!("../source-files/prelude.schala");
|
||||
let mut env = Schala::new_blank_env();
|
||||
|
||||
@ -88,6 +95,22 @@ impl Schala {
|
||||
.typecheck(&ast)
|
||||
.map_err(SchalaError::from_type_error);
|
||||
|
||||
let reduced_ir = reduced_ir::reduce(&ast, &self.symbol_table);
|
||||
println!("Reduced IR: {:?}", reduced_ir);
|
||||
|
||||
let evaluation_outputs = self.eval_state.evaluate(reduced_ir, true);
|
||||
let text_output: Result<Vec<String>, String> = evaluation_outputs.into_iter().collect();
|
||||
|
||||
let text_output: Result<Vec<String>, SchalaError> =
|
||||
text_output.map_err(|err| SchalaError::from_string(err, Stage::Evaluation));
|
||||
|
||||
let eval_output: String =
|
||||
text_output.map(|v| Iterator::intersperse(v.into_iter(), "\n".to_owned()).collect())?;
|
||||
|
||||
Ok(eval_output)
|
||||
|
||||
/*
|
||||
|
||||
// Reduce AST - TODO this doesn't produce an error yet, but probably should
|
||||
let reduced_ast = reduced_ast::reduce(&ast, &self.symbol_table);
|
||||
|
||||
@ -102,6 +125,7 @@ impl Schala {
|
||||
text_output.map(|v| Iterator::intersperse(v.into_iter(), "\n".to_owned()).collect())?;
|
||||
|
||||
Ok(eval_output)
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
@ -151,7 +175,7 @@ fn stage_names() -> Vec<&'static str> {
|
||||
]
|
||||
}
|
||||
|
||||
impl ProgrammingLanguageInterface for Schala {
|
||||
impl<'a> ProgrammingLanguageInterface for Schala<'a> {
|
||||
//TODO flesh out Config
|
||||
type Config = ();
|
||||
fn language_name() -> String {
|
||||
|
@ -8,13 +8,49 @@ use crate::ast::{
|
||||
Variant, VariantKind,
|
||||
};
|
||||
use crate::tokenizing::Location;
|
||||
use crate::typechecking::TypeName;
|
||||
use crate::typechecking::TypeId;
|
||||
|
||||
mod resolver;
|
||||
mod symbol_trie;
|
||||
use symbol_trie::SymbolTrie;
|
||||
mod test;
|
||||
|
||||
|
||||
//TODO parameterize different types of ID
|
||||
/// ID used for definitions
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Clone, Default)]
|
||||
pub struct DefId {
|
||||
idx: u32,
|
||||
}
|
||||
|
||||
impl DefId {
|
||||
pub fn as_u32(&self) -> u32 {
|
||||
self.idx
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for DefId {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "DefId:{}", self.idx)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DefIdStore {
|
||||
last_idx: u32
|
||||
}
|
||||
|
||||
impl DefIdStore {
|
||||
pub fn new() -> Self {
|
||||
Self { last_idx: 0 }
|
||||
}
|
||||
|
||||
pub fn fresh(&mut self) -> DefId {
|
||||
let idx = self.last_idx;
|
||||
self.last_idx += 1;
|
||||
DefId { idx }
|
||||
}
|
||||
}
|
||||
|
||||
/// Fully-qualified symbol name
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
|
||||
pub struct Fqsn {
|
||||
@ -47,6 +83,19 @@ impl Fqsn {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Fqsn {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let delim = "::";
|
||||
let Fqsn { scopes } = self;
|
||||
write!(f, "FQSN<{}", scopes[0])?;
|
||||
for item in scopes[1..].iter() {
|
||||
write!(f, "{}{}", delim, item)?;
|
||||
}
|
||||
write!(f, ">")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//TODO eventually this should use ItemId's to avoid String-cloning
|
||||
/// One segment within a scope.
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
|
||||
@ -54,6 +103,13 @@ enum Scope {
|
||||
Name(Rc<String>),
|
||||
}
|
||||
|
||||
impl fmt::Display for Scope {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let Scope::Name(name) = self;
|
||||
write!(f, "{}", name)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum SymbolError {
|
||||
@ -113,6 +169,9 @@ impl<K> NameTable<K> {
|
||||
|
||||
//cf. p. 150 or so of Language Implementation Patterns
|
||||
pub struct SymbolTable {
|
||||
|
||||
def_id_store: DefIdStore,
|
||||
|
||||
/// Used for import resolution.
|
||||
symbol_trie: SymbolTrie,
|
||||
|
||||
@ -131,6 +190,7 @@ pub struct SymbolTable {
|
||||
impl SymbolTable {
|
||||
pub fn new() -> SymbolTable {
|
||||
SymbolTable {
|
||||
def_id_store: DefIdStore::new(),
|
||||
symbol_trie: SymbolTrie::new(),
|
||||
fq_names: NameTable::new(),
|
||||
types: NameTable::new(),
|
||||
@ -148,67 +208,112 @@ impl SymbolTable {
|
||||
if !errs.is_empty() {
|
||||
return Err(errs);
|
||||
}
|
||||
self.resolve_symbol_ids(ast);
|
||||
self.resolve_scopes(ast);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn lookup_symbol(&self, id: &ItemId) -> Option<&Symbol> {
|
||||
self.id_to_symbol.get(id).map(|s| s.as_ref())
|
||||
}
|
||||
|
||||
//TODO optimize this
|
||||
pub fn lookup_symbol_by_def(&self, def: &DefId) -> Option<&Symbol> {
|
||||
self.id_to_symbol.iter().find(|(_, sym)| sym.def_id == *def)
|
||||
.map(|(_, sym)| sym.as_ref())
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn debug(&self) {
|
||||
println!("Symbol table:");
|
||||
println!("----------------");
|
||||
for (id, sym) in self.id_to_symbol.iter() {
|
||||
println!("{} => {}", id, sym);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Symbol {
|
||||
pub local_name: Rc<String>,
|
||||
fully_qualified_name: Fqsn,
|
||||
pub spec: SymbolSpec,
|
||||
spec: SymbolSpec,
|
||||
def_id: DefId,
|
||||
}
|
||||
|
||||
impl Symbol {
|
||||
pub fn local_name(&self) -> Rc<String> {
|
||||
self.fully_qualified_name.local_name()
|
||||
}
|
||||
|
||||
pub fn def_id(&self) -> Option<DefId> {
|
||||
Some(self.def_id.clone())
|
||||
}
|
||||
|
||||
pub fn spec(&self) -> SymbolSpec {
|
||||
self.spec.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Symbol {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "<Local name: {}, Spec: {}>", self.local_name, self.spec)
|
||||
write!(f, "<Local name: {}, {}, Spec: {}>", self.local_name(), self.fully_qualified_name, self.spec)
|
||||
}
|
||||
}
|
||||
|
||||
//TODO - I think I eventually want to draw a distinction between true global items
|
||||
//i.e. global vars, and items whose definitions are scoped. Right now there's a sense
|
||||
//in which Func, DataConstructor, RecordConstructor, and GlobalBinding are "globals",
|
||||
//whereas LocalVarible and FunctionParam have local scope. But right now, they all
|
||||
//get put into a common table, and all get DefId's from a common source.
|
||||
//
|
||||
//It would be good if individual functions could in parallel look up their own
|
||||
//local vars without interfering with other lookups. Also some type definitions
|
||||
//should be scoped in a similar way.
|
||||
//
|
||||
//Also it makes sense that non-globals should not use DefId's, particularly not
|
||||
//function parameters (even though they are currently assigned).
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum SymbolSpec {
|
||||
Func(Vec<TypeName>),
|
||||
Func,
|
||||
DataConstructor {
|
||||
index: usize,
|
||||
arity: usize,
|
||||
type_name: TypeName, //TODO this eventually needs to be some kind of ID
|
||||
type_id: TypeId,
|
||||
},
|
||||
RecordConstructor {
|
||||
index: usize,
|
||||
members: HashMap<Rc<String>, TypeName>,
|
||||
type_name: TypeName,
|
||||
members: HashMap<Rc<String>, TypeId>,
|
||||
type_id: TypeId,
|
||||
},
|
||||
Binding,
|
||||
GlobalBinding, //Only for global variables, not for function-local ones or ones within a `let` scope context
|
||||
LocalVariable,
|
||||
FunctionParam(u8),
|
||||
}
|
||||
|
||||
impl fmt::Display for SymbolSpec {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
use self::SymbolSpec::*;
|
||||
match self {
|
||||
Func(type_names) => write!(f, "Func({:?})", type_names),
|
||||
Func => write!(f, "Func"),
|
||||
DataConstructor {
|
||||
index,
|
||||
type_name,
|
||||
type_id,
|
||||
arity,
|
||||
} => write!(
|
||||
f,
|
||||
"DataConstructor(idx: {}, arity: {}, type: {})",
|
||||
index, arity, type_name
|
||||
index, arity, type_id
|
||||
),
|
||||
RecordConstructor {
|
||||
type_name, index, ..
|
||||
type_id, index, ..
|
||||
} => write!(
|
||||
f,
|
||||
"RecordConstructor(idx: {})(<members> -> {})",
|
||||
index, type_name
|
||||
index, type_id
|
||||
),
|
||||
Binding => write!(f, "Binding"),
|
||||
GlobalBinding => write!(f, "GlobalBinding"),
|
||||
LocalVariable => write!(f, "Local variable"),
|
||||
FunctionParam(n) => write!(f, "Function param: {}", n),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -220,11 +325,13 @@ impl SymbolTable {
|
||||
/// Register a new mapping of a fully-qualified symbol name (e.g. `Option::Some`)
|
||||
/// to a Symbol, a descriptor of what that name refers to.
|
||||
fn add_symbol(&mut self, id: &ItemId, fqsn: Fqsn, spec: SymbolSpec) {
|
||||
let def_id = self.def_id_store.fresh();
|
||||
let symbol = Rc::new(Symbol {
|
||||
local_name: fqsn.local_name(),
|
||||
fully_qualified_name: fqsn.clone(),
|
||||
spec,
|
||||
def_id,
|
||||
});
|
||||
println!("In add_symbol(), adding: {:?}", symbol);
|
||||
self.symbol_trie.insert(&fqsn);
|
||||
self.fqsn_to_symbol.insert(fqsn, symbol.clone());
|
||||
self.id_to_symbol.insert(id.clone(), symbol.clone());
|
||||
@ -232,8 +339,8 @@ impl SymbolTable {
|
||||
|
||||
/// Walks the AST, matching the ID of an identifier used in some expression to
|
||||
/// the corresponding Symbol.
|
||||
fn resolve_symbol_ids(&mut self, ast: &ast::AST) {
|
||||
let mut resolver = resolver::Resolver::new(self);
|
||||
fn resolve_scopes(&mut self, ast: &ast::AST) {
|
||||
let mut resolver = resolver::ScopeResolver::new(self);
|
||||
resolver.resolve(ast);
|
||||
}
|
||||
|
||||
@ -243,13 +350,14 @@ impl SymbolTable {
|
||||
/// up name tables that will be used by further parts of the compiler
|
||||
fn populate_name_tables(&mut self, ast: &ast::AST) -> Vec<SymbolError> {
|
||||
let mut scope_stack = vec![];
|
||||
self.add_from_scope(ast.statements.as_ref(), &mut scope_stack)
|
||||
self.add_from_scope(ast.statements.as_ref(), &mut scope_stack, false)
|
||||
}
|
||||
|
||||
fn add_from_scope<'a>(
|
||||
&'a mut self,
|
||||
statements: &[Statement],
|
||||
scope_stack: &mut Vec<Scope>,
|
||||
function_scope: bool,
|
||||
) -> Vec<SymbolError> {
|
||||
let mut errors = vec![];
|
||||
|
||||
@ -260,7 +368,7 @@ impl SymbolTable {
|
||||
location,
|
||||
} = statement; //TODO I'm not sure if I need to do anything with this ID
|
||||
let location = *location;
|
||||
if let Err(err) = self.add_single_statement(id, kind, location, scope_stack) {
|
||||
if let Err(err) = self.add_single_statement(id, kind, location, scope_stack, function_scope) {
|
||||
errors.push(err);
|
||||
} else {
|
||||
// If there's an error with a name, don't recurse into subscopes of that name
|
||||
@ -268,14 +376,14 @@ impl SymbolTable {
|
||||
StatementKind::Declaration(Declaration::FuncDecl(signature, body)) => {
|
||||
let new_scope = Scope::Name(signature.name.clone());
|
||||
scope_stack.push(new_scope);
|
||||
let output = self.add_from_scope(body.as_ref(), scope_stack);
|
||||
let output = self.add_from_scope(body.as_ref(), scope_stack, true);
|
||||
scope_stack.pop();
|
||||
output
|
||||
}
|
||||
StatementKind::Module(ModuleSpecifier { name, contents }) => {
|
||||
let new_scope = Scope::Name(name.clone());
|
||||
scope_stack.push(new_scope);
|
||||
let output = self.add_from_scope(contents.as_ref(), scope_stack);
|
||||
let output = self.add_from_scope(contents.as_ref(), scope_stack, false);
|
||||
scope_stack.pop();
|
||||
output
|
||||
}
|
||||
@ -299,6 +407,7 @@ impl SymbolTable {
|
||||
kind: &StatementKind,
|
||||
location: Location,
|
||||
scope_stack: &[Scope],
|
||||
function_scope: bool,
|
||||
) -> Result<(), SymbolError> {
|
||||
match kind {
|
||||
StatementKind::Declaration(Declaration::FuncSig(signature)) => {
|
||||
@ -321,7 +430,7 @@ impl SymbolTable {
|
||||
self.add_symbol(
|
||||
id,
|
||||
fq_function,
|
||||
SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all?
|
||||
SymbolSpec::Func,
|
||||
);
|
||||
}
|
||||
StatementKind::Declaration(Declaration::FuncDecl(signature, ..)) => {
|
||||
@ -345,7 +454,7 @@ impl SymbolTable {
|
||||
self.add_symbol(
|
||||
id,
|
||||
fq_function,
|
||||
SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all?
|
||||
SymbolSpec::Func,
|
||||
);
|
||||
}
|
||||
StatementKind::Declaration(Declaration::TypeDecl { name, .. }) => {
|
||||
@ -367,11 +476,13 @@ impl SymbolTable {
|
||||
kind: NameKind::Binding,
|
||||
},
|
||||
)?;
|
||||
self.add_symbol(
|
||||
id,
|
||||
fq_binding,
|
||||
SymbolSpec::Binding,
|
||||
);
|
||||
if !function_scope {
|
||||
self.add_symbol(
|
||||
id,
|
||||
fq_binding,
|
||||
SymbolSpec::GlobalBinding,
|
||||
);
|
||||
}
|
||||
}
|
||||
StatementKind::Module(ModuleSpecifier { name, .. }) => {
|
||||
let fq_module = Fqsn::from_scope_stack(scope_stack, name.clone());
|
||||
@ -417,13 +528,15 @@ impl SymbolTable {
|
||||
|
||||
for (index, variant) in variants.iter().enumerate() {
|
||||
let Variant { name, kind, id } = variant;
|
||||
let type_id = TypeId::lookup_name(name.as_ref());
|
||||
|
||||
match kind {
|
||||
VariantKind::UnitStruct => {
|
||||
let fq_name = Fqsn::from_scope_stack(scope_stack.as_ref(), name.clone());
|
||||
let spec = SymbolSpec::DataConstructor {
|
||||
index,
|
||||
arity: 0,
|
||||
type_name: name.clone(),
|
||||
type_id,
|
||||
};
|
||||
register(id, fq_name, spec);
|
||||
}
|
||||
@ -432,7 +545,7 @@ impl SymbolTable {
|
||||
let spec = SymbolSpec::DataConstructor {
|
||||
index,
|
||||
arity: items.len(),
|
||||
type_name: name.clone(),
|
||||
type_id,
|
||||
};
|
||||
register(id, fq_name, spec);
|
||||
}
|
||||
@ -459,13 +572,13 @@ impl SymbolTable {
|
||||
|
||||
let spec = SymbolSpec::RecordConstructor {
|
||||
index,
|
||||
type_name: name.clone(),
|
||||
type_id,
|
||||
members: members
|
||||
.iter()
|
||||
.map(|(_, _)| {
|
||||
.map(|(member_name, _type_identifier)| {
|
||||
(
|
||||
Rc::new("DUMMY_FIELD".to_string()),
|
||||
Rc::new("DUMMY_TYPE_ID".to_string()),
|
||||
member_name.clone(),
|
||||
TypeId::lookup_name("DUMMY_TYPE_ID"),
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
|
@ -1,28 +1,49 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::ast::*;
|
||||
use crate::symbol_table::{Fqsn, Scope, SymbolTable};
|
||||
use crate::symbol_table::{Fqsn, Scope, SymbolTable, SymbolSpec};
|
||||
use crate::util::ScopeStack;
|
||||
|
||||
type FqsnPrefix = Vec<Scope>;
|
||||
|
||||
pub struct Resolver<'a> {
|
||||
symbol_table: &'a mut super::SymbolTable,
|
||||
name_scope_stack: ScopeStack<'a, Rc<String>, FqsnPrefix>,
|
||||
#[derive(Debug)]
|
||||
enum NameType {
|
||||
//TODO eventually this needs to support closures
|
||||
Param(u8), //TODO handle implications of functions being limited to 255 params
|
||||
LocalVariable(ItemId),
|
||||
Import(Fqsn),
|
||||
}
|
||||
|
||||
impl<'a> Resolver<'a> {
|
||||
#[derive(Debug)]
|
||||
enum ScopeType {
|
||||
Function {
|
||||
name: Rc<String>
|
||||
},
|
||||
Lambda,
|
||||
PatternMatch,
|
||||
//TODO add some notion of a let-like scope?
|
||||
}
|
||||
|
||||
pub struct ScopeResolver<'a> {
|
||||
symbol_table: &'a mut super::SymbolTable,
|
||||
//TODO maybe this shouldn't be a scope stack, b/c the recursion behavior comes from multiple
|
||||
//instances of ScopeResolver
|
||||
lexical_scopes: ScopeStack<'a, Rc<String>, NameType, ScopeType>,
|
||||
}
|
||||
|
||||
impl<'a> ScopeResolver<'a> {
|
||||
pub fn new(symbol_table: &'a mut SymbolTable) -> Self {
|
||||
let name_scope_stack: ScopeStack<'a, Rc<String>, FqsnPrefix> = ScopeStack::new(None);
|
||||
let lexical_scopes = ScopeStack::new(None);
|
||||
Self {
|
||||
symbol_table,
|
||||
name_scope_stack,
|
||||
lexical_scopes,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resolve(&mut self, ast: &AST) {
|
||||
walk_ast(self, ast);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
fn lookup_name_in_scope(&self, sym_name: &QualifiedName) -> Fqsn {
|
||||
let QualifiedName { components, .. } = sym_name;
|
||||
let first_component = &components[0];
|
||||
@ -45,19 +66,63 @@ impl<'a> Resolver<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
fn qualified_name(&mut self, name: &QualifiedName) {
|
||||
let fqsn = self.lookup_name_in_scope(name);
|
||||
let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn);
|
||||
if let Some(symbol) = symbol {
|
||||
self.symbol_table.id_to_symbol.insert(name.id.clone(), symbol.clone());
|
||||
/// This method correctly modifies the id_to_symbol table (ItemId) to have the appropriate
|
||||
/// mappings.
|
||||
fn lookup_name_in_scope(&mut self, name: &QualifiedName) {
|
||||
let QualifiedName { id, components } = name;
|
||||
//TODO handle a "partial" qualified name, and also handle it down in the pattern-matching
|
||||
//section
|
||||
//TODO some of these if lets that look into the fqsn_to_symbol table should probaby fail
|
||||
//with an error
|
||||
if components.len() == 1 {
|
||||
let local_name: Rc<String> = components[0].clone();
|
||||
let name_type = self.lexical_scopes.lookup(&local_name);
|
||||
match name_type {
|
||||
Some(NameType::Import(fqsn)) => {
|
||||
let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn);
|
||||
if let Some(symbol) = symbol {
|
||||
self.symbol_table.id_to_symbol.insert(id.clone(), symbol.clone());
|
||||
}
|
||||
},
|
||||
Some(NameType::Param(n)) => {
|
||||
let spec = SymbolSpec::FunctionParam(*n);
|
||||
//TODO need to come up with a better solution for local variable FQSNs
|
||||
let lscope = Scope::Name(Rc::new("<local-param>".to_string()));
|
||||
let fqsn = Fqsn { scopes: vec![lscope, Scope::Name(local_name.clone())] };
|
||||
self.symbol_table.add_symbol(id, fqsn, spec);
|
||||
}
|
||||
Some(NameType::LocalVariable(item_id)) => {
|
||||
let symbol = self.symbol_table.id_to_symbol.get(&item_id).cloned();
|
||||
if let Some(symbol) = symbol {
|
||||
self.symbol_table.id_to_symbol.insert(id.clone(), symbol);
|
||||
}
|
||||
},
|
||||
None => {
|
||||
//TODO see if I can reduce this duplicate code
|
||||
let fqsn = Fqsn { scopes: vec![Scope::Name(local_name.clone())] };
|
||||
let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn);
|
||||
if let Some(symbol) = symbol {
|
||||
self.symbol_table.id_to_symbol.insert(id.clone(), symbol.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let fqsn = Fqsn { scopes: components.iter().map(|name| Scope::Name(name.clone())).collect() };
|
||||
let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn);
|
||||
if let Some(symbol) = symbol {
|
||||
self.symbol_table.id_to_symbol.insert(id.clone(), symbol.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ASTVisitor for Resolver<'a> {
|
||||
//TODO need to un-insert these - maybe need to rethink visitor
|
||||
fn import(&mut self, import_spec: &ImportSpecifier) {
|
||||
impl<'a> ASTVisitor for ScopeResolver<'a> {
|
||||
// Import statements bring in a bunch of local names that all map to a specific FQSN.
|
||||
// FQSNs map to a Symbol (or this is an error), Symbols have a DefId. So for every
|
||||
// name we import, we map a local name (a string) to a NameType::ImportedDefinition(DefId).
|
||||
fn import(&mut self, import_spec: &ImportSpecifier) -> Recursion {
|
||||
let ImportSpecifier {
|
||||
ref path_components,
|
||||
ref imported_names,
|
||||
@ -72,56 +137,185 @@ impl<'a> ASTVisitor for Resolver<'a> {
|
||||
.collect(),
|
||||
};
|
||||
let members = self.symbol_table.symbol_trie.get_children(&prefix);
|
||||
for member in members.into_iter() {
|
||||
let Scope::Name(n) = member.scopes.last().unwrap();
|
||||
let local_name = n.clone();
|
||||
self.name_scope_stack.insert(local_name, member.scopes);
|
||||
for fqsn in members.into_iter() {
|
||||
self.lexical_scopes.insert( fqsn.local_name(), NameType::Import(fqsn));
|
||||
}
|
||||
}
|
||||
ImportedNames::LastOfPath => {
|
||||
let name = path_components.last().unwrap(); //TODO handle better
|
||||
let fqsn_prefix = path_components
|
||||
let fqsn = Fqsn {
|
||||
scopes: path_components
|
||||
.iter()
|
||||
.map(|c| Scope::Name(c.clone()))
|
||||
.collect();
|
||||
self.name_scope_stack.insert(name.clone(), fqsn_prefix);
|
||||
.collect()
|
||||
};
|
||||
self.lexical_scopes.insert(fqsn.local_name(), NameType::Import(fqsn));
|
||||
}
|
||||
ImportedNames::List(ref names) => {
|
||||
let fqsn_prefix: FqsnPrefix = path_components
|
||||
let fqsn_prefix: Vec<Scope> = path_components
|
||||
.iter()
|
||||
.map(|c| Scope::Name(c.clone()))
|
||||
.collect();
|
||||
for name in names.iter() {
|
||||
self.name_scope_stack
|
||||
.insert(name.clone(), fqsn_prefix.clone());
|
||||
let mut scopes = fqsn_prefix.clone();
|
||||
scopes.push(Scope::Name(name.clone()));
|
||||
let fqsn = Fqsn { scopes };
|
||||
self.lexical_scopes.insert(fqsn.local_name(), NameType::Import(fqsn));
|
||||
}
|
||||
}
|
||||
};
|
||||
Recursion::Continue
|
||||
}
|
||||
|
||||
fn expression(&mut self, expression: &Expression) {
|
||||
use ExpressionKind::*;
|
||||
match &expression.kind {
|
||||
Value(name) => {
|
||||
self.qualified_name(name);
|
||||
},
|
||||
NamedStruct { name, fields: _ } => {
|
||||
self.qualified_name(name);
|
||||
},
|
||||
_ => (),
|
||||
fn declaration(&mut self, declaration: &Declaration, id: &ItemId) -> Recursion {
|
||||
let cur_function_name = match self.lexical_scopes.get_name() {
|
||||
//TODO this needs to be a fqsn
|
||||
Some(ScopeType::Function { name }) => Some(name.clone()),
|
||||
_ => None
|
||||
};
|
||||
match declaration {
|
||||
Declaration::FuncDecl(signature, block) => {
|
||||
let param_names = signature.params.iter().map(|param| param.name.clone());
|
||||
//TODO I'm 90% sure this is right, until I get to closures
|
||||
//let mut new_scope = self.lexical_scopes.new_scope(Some(ScopeType::Function { name: signature.name.clone() }));
|
||||
let mut new_scope = ScopeStack::new(Some(ScopeType::Function { name: signature.name.clone() }));
|
||||
|
||||
for (n, param) in param_names.enumerate().into_iter() {
|
||||
new_scope.insert(param, NameType::Param(n as u8));
|
||||
}
|
||||
|
||||
let mut new_resolver = ScopeResolver {
|
||||
symbol_table: self.symbol_table,
|
||||
lexical_scopes: new_scope,
|
||||
};
|
||||
walk_block(&mut new_resolver, block);
|
||||
Recursion::Stop
|
||||
}
|
||||
Declaration::Binding { name, .. } => {
|
||||
if let Some(fn_name) = cur_function_name {
|
||||
// We are within a function scope
|
||||
let fqsn = Fqsn { scopes: vec![Scope::Name(fn_name.clone()), Scope::Name(name.clone())] };
|
||||
self.symbol_table.add_symbol(id, fqsn, SymbolSpec::LocalVariable);
|
||||
self.lexical_scopes.insert(name.clone(), NameType::LocalVariable(id.clone()));
|
||||
}
|
||||
Recursion::Continue
|
||||
}
|
||||
_ => Recursion::Continue
|
||||
}
|
||||
}
|
||||
|
||||
fn pattern(&mut self, pat: &Pattern) {
|
||||
fn expression(&mut self, expression: &Expression) -> Recursion {
|
||||
use ExpressionKind::*;
|
||||
match &expression.kind {
|
||||
Value(name) => {
|
||||
self.lookup_name_in_scope(name);
|
||||
},
|
||||
NamedStruct { name, fields: _ } => {
|
||||
self.lookup_name_in_scope(name);
|
||||
},
|
||||
Lambda { params, body, .. } => {
|
||||
let param_names = params.iter().map(|param| param.name.clone());
|
||||
//TODO need to properly handle closure scope, this is currently broken
|
||||
//let mut new_scope = self.lexical_scopes.new_scope(Some(ScopeType::Function { name: signature.name.clone() }));
|
||||
let mut new_scope = ScopeStack::new(Some(ScopeType::Lambda));
|
||||
|
||||
for (n, param) in param_names.enumerate().into_iter() {
|
||||
new_scope.insert(param, NameType::Param(n as u8));
|
||||
}
|
||||
|
||||
let mut new_resolver = ScopeResolver {
|
||||
symbol_table: self.symbol_table,
|
||||
lexical_scopes: new_scope,
|
||||
};
|
||||
walk_block(&mut new_resolver, body);
|
||||
return Recursion::Stop;
|
||||
}
|
||||
IfExpression { discriminator, body } => {
|
||||
if let Some(d) = discriminator.as_ref() {
|
||||
walk_expression(self, &d);
|
||||
}
|
||||
let mut resolver = ScopeResolver {
|
||||
lexical_scopes: self.lexical_scopes.new_scope(Some(ScopeType::PatternMatch)),
|
||||
symbol_table: self.symbol_table
|
||||
};
|
||||
let new_resolver = &mut resolver;
|
||||
|
||||
match body.as_ref() {
|
||||
IfExpressionBody::SimpleConditional {
|
||||
then_case,
|
||||
else_case,
|
||||
} => {
|
||||
walk_block(new_resolver, then_case);
|
||||
if let Some(block) = else_case.as_ref() {
|
||||
walk_block(new_resolver, block)
|
||||
}
|
||||
}
|
||||
IfExpressionBody::SimplePatternMatch {
|
||||
pattern,
|
||||
then_case,
|
||||
else_case,
|
||||
} => {
|
||||
walk_pattern(new_resolver, pattern);
|
||||
walk_block(new_resolver, &then_case);
|
||||
if let Some(ref block) = else_case.as_ref() {
|
||||
walk_block(new_resolver, &block)
|
||||
}
|
||||
}
|
||||
IfExpressionBody::CondList(arms) => {
|
||||
for arm in arms {
|
||||
match arm.condition {
|
||||
Condition::Pattern(ref pat) => {
|
||||
walk_pattern(new_resolver, pat);
|
||||
}
|
||||
Condition::TruncatedOp(ref _binop, ref expr) => {
|
||||
walk_expression(new_resolver, expr);
|
||||
}
|
||||
Condition::Expression(ref expr) => {
|
||||
walk_expression(new_resolver, expr);
|
||||
}
|
||||
Condition::Else => (),
|
||||
}
|
||||
if let Some(ref guard) = arm.guard {
|
||||
walk_expression(new_resolver, &guard);
|
||||
}
|
||||
walk_block(new_resolver, &arm.body);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return Recursion::Stop;
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
Recursion::Continue
|
||||
}
|
||||
|
||||
fn pattern(&mut self, pat: &Pattern) -> Recursion {
|
||||
use Pattern::*;
|
||||
|
||||
match pat {
|
||||
//TODO I think not handling TuplePattern is an oversight
|
||||
TuplePattern(_) => (),
|
||||
Literal(_) | Ignored => (),
|
||||
TupleStruct(name, _) | Record(name, _) | VarOrName(name) => {
|
||||
self.qualified_name(name);
|
||||
Literal(..) | Ignored | TuplePattern(..) => (),
|
||||
TupleStruct(name, _) | Record(name, _) => {
|
||||
self.lookup_name_in_scope(name);
|
||||
}
|
||||
//TODO this isn't really the right syntax for a VarOrName
|
||||
VarOrName(ref name @ QualifiedName { id, components }) => {
|
||||
if components.len() == 1 {
|
||||
//TODO need a better way to construct a FQSN from a QualifiedName
|
||||
let local_name: Rc<String> = components[0].clone();
|
||||
let lscope = Scope::Name(Rc::new("<local-case-match>".to_string()));
|
||||
let fqsn = Fqsn { scopes: vec![lscope, Scope::Name(local_name.clone())] };
|
||||
//let local_name = fqsn.local_name();
|
||||
self.symbol_table.add_symbol(id, fqsn, SymbolSpec::LocalVariable);
|
||||
self.lexical_scopes.insert(local_name.clone(), NameType::LocalVariable(id.clone()));
|
||||
} else {
|
||||
let fqsn = Fqsn { scopes: components.iter().map(|name| Scope::Name(name.clone())).collect() };
|
||||
let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn);
|
||||
if let Some(symbol) = symbol {
|
||||
self.symbol_table.id_to_symbol.insert(id.clone(), symbol.clone());
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
Recursion::Continue
|
||||
}
|
||||
}
|
||||
|
471
schala-lang/language/src/tree_walk_eval/mod.rs
Normal file
471
schala-lang/language/src/tree_walk_eval/mod.rs
Normal file
@ -0,0 +1,471 @@
|
||||
use crate::reduced_ir::{ReducedIR, Expression, Lookup, Callable, FunctionDefinition, Statement, Literal, Alternative, Pattern};
|
||||
use crate::symbol_table::{DefId};
|
||||
use crate::util::ScopeStack;
|
||||
use crate::builtin::Builtin;
|
||||
use crate::typechecking::TypeId;
|
||||
|
||||
use std::fmt::Write;
|
||||
use std::rc::Rc;
|
||||
use std::convert::From;
|
||||
|
||||
mod test;
|
||||
|
||||
type EvalResult<T> = Result<T, RuntimeError>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct State<'a> {
|
||||
environments: ScopeStack<'a, Memory, MemoryValue>,
|
||||
}
|
||||
|
||||
//TODO - eh, I dunno, maybe it doesn't matter exactly how memory works in the tree-walking
|
||||
//evaluator
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
|
||||
enum Memory {
|
||||
Index(u32)
|
||||
}
|
||||
|
||||
// This is for function param lookups, and is a hack
|
||||
impl From<u8> for Memory {
|
||||
fn from(n: u8) -> Self {
|
||||
Memory::Index(4_000_000 + (n as u32))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&DefId> for Memory {
|
||||
fn from(id: &DefId) -> Self {
|
||||
Self::Index(id.as_u32())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct RuntimeError {
|
||||
msg: String
|
||||
}
|
||||
|
||||
impl From<String> for RuntimeError {
|
||||
fn from(msg: String) -> Self {
|
||||
Self {
|
||||
msg
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for RuntimeError {
|
||||
fn from(msg: &str) -> Self {
|
||||
Self {
|
||||
msg: msg.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RuntimeError {
|
||||
#[allow(dead_code)]
|
||||
fn get_msg(&self) -> String {
|
||||
format!("Runtime error: {}", self.msg)
|
||||
}
|
||||
}
|
||||
|
||||
fn paren_wrapped(terms: impl Iterator<Item=String>) -> String {
|
||||
let mut buf = String::new();
|
||||
write!(buf, "(").unwrap();
|
||||
for term in terms.map(Some).intersperse(None) {
|
||||
match term {
|
||||
Some(e) => write!(buf, "{}", e).unwrap(),
|
||||
None => write!(buf, ", ").unwrap(),
|
||||
};
|
||||
}
|
||||
write!(buf, ")").unwrap();
|
||||
buf
|
||||
}
|
||||
|
||||
/// Anything that can be stored in memory; that is, a function definition, or a fully-evaluated
|
||||
/// program value.
|
||||
#[derive(Debug)]
|
||||
enum MemoryValue {
|
||||
Function(FunctionDefinition),
|
||||
Primitive(Primitive),
|
||||
}
|
||||
|
||||
impl From<Primitive> for MemoryValue {
|
||||
fn from(prim: Primitive) -> Self {
|
||||
Self::Primitive(prim)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl MemoryValue {
|
||||
fn to_repl(&self) -> String {
|
||||
match self {
|
||||
MemoryValue::Primitive(ref prim) => prim.to_repl(),
|
||||
MemoryValue::Function(..) => "<function>".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum RuntimeValue {
|
||||
Expression(Expression),
|
||||
Evaluated(Primitive),
|
||||
}
|
||||
|
||||
impl From<Expression> for RuntimeValue {
|
||||
fn from(expr: Expression) -> Self {
|
||||
Self::Expression(expr)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Primitive> for RuntimeValue {
|
||||
fn from(prim: Primitive) -> Self {
|
||||
Self::Evaluated(prim)
|
||||
}
|
||||
}
|
||||
|
||||
/// A fully-reduced value
|
||||
#[derive(Debug, Clone)]
|
||||
enum Primitive {
|
||||
Tuple(Vec<Primitive>),
|
||||
Literal(Literal),
|
||||
Callable(Callable),
|
||||
Object {
|
||||
type_id: TypeId,
|
||||
tag: u32,
|
||||
items: Vec<Primitive>
|
||||
},
|
||||
}
|
||||
|
||||
impl Primitive {
|
||||
fn to_repl(&self) -> String {
|
||||
match self {
|
||||
Primitive::Object { type_id, items, .. } if items.len() == 0 => format!("{}", type_id.local_name()),
|
||||
Primitive::Object { type_id, items, .. } =>
|
||||
format!("{}{}", type_id.local_name(), paren_wrapped(items.iter().map(|item| item.to_repl()))),
|
||||
Primitive::Literal(lit) => match lit {
|
||||
Literal::Nat(n) => format!("{}", n),
|
||||
Literal::Int(i) => format!("{}", i),
|
||||
Literal::Float(f) => format!("{}", f),
|
||||
Literal::Bool(b) => format!("{}", b),
|
||||
Literal::StringLit(s) => format!("\"{}\"", s),
|
||||
}
|
||||
Primitive::Tuple(terms) => paren_wrapped(terms.iter().map(|x| x.to_repl())),
|
||||
Primitive::Callable(..) => "<some-callable>".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn unit() -> Self {
|
||||
Primitive::Tuple(vec![])
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Literal> for Primitive {
|
||||
fn from(lit: Literal) -> Self {
|
||||
Primitive::Literal(lit)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> State<'a> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
environments: ScopeStack::new(Some("global".to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn evaluate(&mut self, reduced: ReducedIR, repl: bool) -> Vec<Result<String, String>> {
|
||||
let mut acc = vec![];
|
||||
|
||||
for (def_id, function) in reduced.functions.into_iter() {
|
||||
let mem = (&def_id).into();
|
||||
self.environments.insert(mem, MemoryValue::Function(function));
|
||||
}
|
||||
|
||||
for statement in reduced.entrypoint.into_iter() {
|
||||
match self.statement(statement) {
|
||||
Ok(Some(output)) if repl => {
|
||||
acc.push(Ok(output.to_repl()))
|
||||
},
|
||||
Ok(_) => (),
|
||||
Err(error) => {
|
||||
acc.push(Err(error.msg));
|
||||
return acc;
|
||||
}
|
||||
}
|
||||
}
|
||||
acc
|
||||
}
|
||||
|
||||
fn block(&mut self, statements: Vec<Statement>) -> EvalResult<Primitive> {
|
||||
//TODO need to handle breaks, returns, etc.
|
||||
let mut ret = None;
|
||||
for stmt in statements.into_iter() {
|
||||
if let Some(MemoryValue::Primitive(prim)) = self.statement(stmt)? {
|
||||
ret = Some(prim);
|
||||
}
|
||||
}
|
||||
Ok(if let Some(ret) = ret {
|
||||
ret
|
||||
} else {
|
||||
self.expression(Expression::unit())?
|
||||
})
|
||||
}
|
||||
|
||||
fn statement(&mut self, stmt: Statement) -> EvalResult<Option<MemoryValue>> {
|
||||
match stmt {
|
||||
Statement::Binding { ref id, expr, constant: _ } => {
|
||||
println!("eval() binding id: {}", id);
|
||||
let evaluated = self.expression(expr)?;
|
||||
self.environments.insert(id.into(), evaluated.into());
|
||||
Ok(None)
|
||||
},
|
||||
Statement::Expression(expr) => {
|
||||
let evaluated = self.expression(expr)?;
|
||||
Ok(Some(evaluated.into()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn expression(&mut self, expression: Expression) -> EvalResult<Primitive> {
|
||||
Ok(match expression {
|
||||
Expression::Literal(lit) => Primitive::Literal(lit),
|
||||
Expression::Tuple(items) => Primitive::Tuple(items.into_iter().map(|expr| self.expression(expr)).collect::<EvalResult<Vec<Primitive>>>()?),
|
||||
Expression::Lookup(kind) => match kind {
|
||||
Lookup::Function(ref id) => {
|
||||
let mem = id.into();
|
||||
match self.environments.lookup(&mem) {
|
||||
// This just checks that the function exists in "memory" by ID, we don't
|
||||
// actually retrieve it until `apply_function()`
|
||||
Some(MemoryValue::Function(_)) => Primitive::Callable(Callable::UserDefined(id.clone())),
|
||||
x => return Err(format!("Function not found for id: {} : {:?}", id, x).into()),
|
||||
}
|
||||
},
|
||||
Lookup::Param(n) => {
|
||||
let mem = n.into();
|
||||
match self.environments.lookup(&mem) {
|
||||
Some(MemoryValue::Primitive(prim)) => prim.clone(),
|
||||
e => return Err(format!("Param lookup error, got {:?}", e).into()),
|
||||
}
|
||||
},
|
||||
Lookup::LocalVar(ref id) | Lookup::GlobalVar(ref id) => {
|
||||
let mem = id.into();
|
||||
match self.environments.lookup(&mem) {
|
||||
Some(MemoryValue::Primitive(expr)) => expr.clone(),
|
||||
_ => return Err(format!("Nothing found for local/gloval variable lookup {}", id).into()),
|
||||
}
|
||||
},
|
||||
},
|
||||
Expression::Assign { ref lval, box rval } => {
|
||||
let mem = lval.into();
|
||||
let evaluated = self.expression(rval)?;
|
||||
self.environments.insert(mem, MemoryValue::Primitive(evaluated));
|
||||
Primitive::unit()
|
||||
},
|
||||
Expression::Call { box f, args } => self.call_expression(f, args)?,
|
||||
Expression::Callable(Callable::DataConstructor { type_id, arity, tag }) if arity == 0 => Primitive::Object {
|
||||
type_id, tag, items: vec![]
|
||||
},
|
||||
Expression::Callable(func) => Primitive::Callable(func),
|
||||
Expression::Conditional { box cond, then_clause, else_clause } => {
|
||||
let cond = self.expression(cond)?;
|
||||
match cond {
|
||||
Primitive::Literal(Literal::Bool(true)) => self.block(then_clause)?,
|
||||
Primitive::Literal(Literal::Bool(false)) => self.block(else_clause)?,
|
||||
v => return Err(format!("Non-boolean value {:?} in if-statement", v).into())
|
||||
}
|
||||
},
|
||||
Expression::CaseMatch { box cond, alternatives } => self.case_match_expression(cond, alternatives)?,
|
||||
Expression::ReductionError(e) => return Err(e.into()),
|
||||
})
|
||||
}
|
||||
|
||||
fn case_match_expression(&mut self, cond: Expression, alternatives: Vec<Alternative>) -> EvalResult<Primitive> {
|
||||
fn matches(scrut: &Primitive, pat: &Pattern, scope: &mut ScopeStack<Memory, MemoryValue>) -> bool {
|
||||
match pat {
|
||||
Pattern::Ignored => true,
|
||||
Pattern::Binding(ref def_id) => {
|
||||
let mem = def_id.into();
|
||||
scope.insert(mem, MemoryValue::Primitive(scrut.clone())); //TODO make sure this doesn't cause problems with nesting
|
||||
true
|
||||
},
|
||||
Pattern::Literal(pat_literal) => if let Primitive::Literal(scrut_literal) = scrut {
|
||||
pat_literal == scrut_literal
|
||||
} else {
|
||||
false
|
||||
},
|
||||
Pattern::Tuple { subpatterns, tag } => match tag {
|
||||
None => match scrut {
|
||||
Primitive::Tuple(items) if items.len() == subpatterns.len() =>
|
||||
items.iter().zip(subpatterns.iter()).all(|(item, subpat)| matches(item, subpat, scope)),
|
||||
_ => false //TODO should be a type error
|
||||
},
|
||||
Some(pattern_tag) => match scrut {
|
||||
//TODO should test type_ids for runtime type checking, once those work
|
||||
Primitive::Object { tag, items, .. } if tag == pattern_tag && items.len() == subpatterns.len() => {
|
||||
items.iter().zip(subpatterns.iter()).all(|(item, subpat)| matches(item, subpat, scope))
|
||||
}
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let cond = self.expression(cond)?;
|
||||
|
||||
for alt in alternatives.into_iter() {
|
||||
let mut new_scope = self.environments.new_scope(None);
|
||||
if matches(&cond, &alt.pattern, &mut new_scope) {
|
||||
let mut new_state = State {
|
||||
environments: new_scope
|
||||
};
|
||||
|
||||
return new_state.block(alt.item)
|
||||
}
|
||||
}
|
||||
Err("No valid match in match expression".into())
|
||||
}
|
||||
|
||||
fn call_expression(&mut self, f: Expression, args: Vec<Expression>) -> EvalResult<Primitive> {
|
||||
let func = match self.expression(f)? {
|
||||
Primitive::Callable(func) => func,
|
||||
other => return Err(format!("Trying to call non-function value: {:?}", other).into()),
|
||||
};
|
||||
match func {
|
||||
Callable::Builtin(builtin) => self.apply_builtin(builtin, args),
|
||||
Callable::UserDefined(def_id) => {
|
||||
let mem = (&def_id).into();
|
||||
match self.environments.lookup(&mem) {
|
||||
Some(MemoryValue::Function(FunctionDefinition { body })) => {
|
||||
let body = body.clone(); //TODO ideally this clone would not happen
|
||||
self.apply_function(body, args)
|
||||
},
|
||||
e => Err(format!("Error looking up function with id {}: {:?}", def_id, e).into())
|
||||
}
|
||||
},
|
||||
Callable::Lambda { arity, body } => {
|
||||
if arity as usize != args.len() {
|
||||
return Err(format!("Lambda expression requries {} arguments, only {} provided", arity, args.len()).into());
|
||||
}
|
||||
let body = body.clone(); //TODO again ideally, no cloning here
|
||||
self.apply_function(body, args)
|
||||
}
|
||||
Callable::DataConstructor { type_id, arity, tag } => {
|
||||
if arity as usize != args.len() {
|
||||
return Err(format!("Constructor expression requries {} arguments, only {} provided", arity, args.len()).into());
|
||||
}
|
||||
|
||||
let mut evaluated_args: Vec<Primitive> = vec![];
|
||||
for arg in args.into_iter() {
|
||||
evaluated_args.push(self.expression(arg)?);
|
||||
}
|
||||
Ok(Primitive::Object {
|
||||
type_id,
|
||||
tag,
|
||||
items: evaluated_args
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_builtin(&mut self, builtin: Builtin, args: Vec<Expression>) -> EvalResult<Primitive> {
|
||||
use Builtin::*;
|
||||
use Literal::*;
|
||||
use Primitive::Literal as Lit;
|
||||
|
||||
let evaled_args: EvalResult<Vec<Primitive>> =
|
||||
args.into_iter().map(|arg| self.expression(arg)).collect();
|
||||
let evaled_args = evaled_args?;
|
||||
|
||||
Ok(match (builtin, evaled_args.as_slice()) {
|
||||
(FieldAccess, /*&[Node::PrimObject { .. }]*/ _) => {
|
||||
return Err("Field access unimplemented".into());
|
||||
}
|
||||
/* builtin functions */
|
||||
(IOPrint, &[ref anything]) => {
|
||||
print!("{}", anything.to_repl());
|
||||
Primitive::Tuple(vec![])
|
||||
},
|
||||
(IOPrintLn, &[ref anything]) => {
|
||||
print!("{}", anything.to_repl());
|
||||
Primitive::Tuple(vec![])
|
||||
},
|
||||
(IOGetLine, &[]) => {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).expect("Error readling line in 'getline'");
|
||||
StringLit(Rc::new(buf.trim().to_string())).into()
|
||||
},
|
||||
/* Binops */
|
||||
(binop, &[ref lhs, ref rhs]) => match (binop, lhs, rhs) {
|
||||
// TODO need a better way of handling these literals
|
||||
(Add, Lit(Nat(l)), Lit(Nat(r))) => Nat(l + r).into(),
|
||||
(Add, Lit(Int(l)), Lit(Int(r))) => Int(l + r).into(),
|
||||
(Add, Lit(Nat(l)), Lit(Int(r))) => Int((*l as i64) + (*r as i64)).into(),
|
||||
(Add, Lit(Int(l)), Lit(Nat(r))) => Int((*l as i64) + (*r as i64)).into(),
|
||||
(Concatenate, Lit(StringLit(ref s1)), Lit(StringLit(ref s2))) => StringLit(Rc::new(format!("{}{}", s1, s2))).into(),
|
||||
(Subtract, Lit(Nat(l)), Lit(Nat(r))) => Nat(l - r).into(),
|
||||
(Multiply, Lit(Nat(l)), Lit(Nat(r))) => Nat(l * r).into(),
|
||||
(Divide, Lit(Nat(l)), Lit(Nat(r))) => Float((*l as f64)/ (*r as f64)).into(),
|
||||
(Quotient, Lit(Nat(l)), Lit(Nat(r))) => if *r == 0 {
|
||||
return Err("Divide-by-zero error".into());
|
||||
} else {
|
||||
Nat(l / r).into()
|
||||
},
|
||||
(Modulo, Lit(Nat(l)), Lit(Nat(r))) => Nat(l % r).into(),
|
||||
(Exponentiation, Lit(Nat(l)), Lit(Nat(r))) => Nat(l ^ r).into(),
|
||||
(BitwiseAnd, Lit(Nat(l)), Lit(Nat(r))) => Nat(l & r).into(),
|
||||
(BitwiseOr, Lit(Nat(l)), Lit(Nat(r))) => Nat(l | r).into(),
|
||||
|
||||
/* comparisons */
|
||||
(Equality, Lit(Nat(l)), Lit(Nat(r))) => Bool(l == r).into(),
|
||||
(Equality, Lit(Int(l)), Lit(Int(r))) => Bool(l == r).into(),
|
||||
(Equality, Lit(Float(l)), Lit(Float(r))) => Bool(l == r).into(),
|
||||
(Equality, Lit(Bool(l)), Lit(Bool(r))) => Bool(l == r).into(),
|
||||
(Equality, Lit(StringLit(ref l)), Lit(StringLit(ref r))) => Bool(l == r).into(),
|
||||
|
||||
(LessThan, Lit(Nat(l)), Lit(Nat(r))) => Bool(l < r).into(),
|
||||
(LessThan, Lit(Int(l)), Lit(Int(r))) => Bool(l < r).into(),
|
||||
(LessThan, Lit(Float(l)), Lit(Float(r))) => Bool(l < r).into(),
|
||||
|
||||
(LessThanOrEqual, Lit(Nat(l)), Lit(Nat(r))) => Bool(l <= r).into(),
|
||||
(LessThanOrEqual, Lit(Int(l)), Lit(Int(r))) => Bool(l <= r).into(),
|
||||
(LessThanOrEqual, Lit(Float(l)), Lit(Float(r))) => Bool(l <= r).into(),
|
||||
|
||||
(GreaterThan, Lit(Nat(l)), Lit(Nat(r))) => Bool(l > r).into(),
|
||||
(GreaterThan, Lit(Int(l)), Lit(Int(r))) => Bool(l > r).into(),
|
||||
(GreaterThan, Lit(Float(l)), Lit(Float(r))) => Bool(l > r).into(),
|
||||
|
||||
(GreaterThanOrEqual, Lit(Nat(l)), Lit(Nat(r))) => Bool(l >= r).into(),
|
||||
(GreaterThanOrEqual, Lit(Int(l)), Lit(Int(r))) => Bool(l >= r).into(),
|
||||
(GreaterThanOrEqual, Lit(Float(l)), Lit(Float(r))) => Bool(l >= r).into(),
|
||||
|
||||
(binop, lhs, rhs) => return Err(format!("Invalid binop expression {:?} {:?} {:?}", lhs, binop, rhs).into()),
|
||||
},
|
||||
(prefix, &[ref arg]) => match (prefix, arg) {
|
||||
(BooleanNot, Lit(Bool(true))) => Bool(false),
|
||||
(BooleanNot, Lit(Bool(false))) => Bool(true),
|
||||
(Negate, Lit(Nat(n))) => Int(-(*n as i64)),
|
||||
(Negate, Lit(Int(n))) => Int(-(*n as i64)),
|
||||
(Negate, Lit(Float(f))) => Float(-(*f as f64)),
|
||||
(Increment, Lit(Int(n))) => Int(*n),
|
||||
(Increment, Lit(Nat(n))) => Nat(*n),
|
||||
_ => return Err("No valid prefix op".into())
|
||||
}.into(),
|
||||
(x, args) => return Err(format!("bad or unimplemented builtin {:?} | {:?}", x, args).into()),
|
||||
})
|
||||
}
|
||||
|
||||
fn apply_function(&mut self, body: Vec<Statement>, args: Vec<Expression>) -> EvalResult<Primitive> {
|
||||
let mut evaluated_args: Vec<Primitive> = vec![];
|
||||
for arg in args.into_iter() {
|
||||
evaluated_args.push(self.expression(arg)?);
|
||||
}
|
||||
|
||||
let mut frame_state = State {
|
||||
environments: self.environments.new_scope(None)
|
||||
};
|
||||
|
||||
for (n, evaled) in evaluated_args.into_iter().enumerate() {
|
||||
let n = n as u8;
|
||||
let mem = n.into();
|
||||
frame_state.environments.insert(mem, MemoryValue::Primitive(evaled));
|
||||
}
|
||||
|
||||
frame_state.block(body)
|
||||
}
|
||||
}
|
||||
|
330
schala-lang/language/src/tree_walk_eval/test.rs
Normal file
330
schala-lang/language/src/tree_walk_eval/test.rs
Normal file
@ -0,0 +1,330 @@
|
||||
#![cfg(test)]
|
||||
use test_case::test_case;
|
||||
|
||||
use crate::symbol_table::SymbolTable;
|
||||
use crate::tree_walk_eval::State;
|
||||
|
||||
fn evaluate_input(input: &str) -> Result<String, String> {
|
||||
let ast = crate::util::quick_ast(input);
|
||||
let mut symbol_table = SymbolTable::new();
|
||||
symbol_table.process_ast(&ast).unwrap();
|
||||
let reduced_ir = crate::reduced_ir::reduce(&ast, &symbol_table);
|
||||
reduced_ir.debug(&symbol_table);
|
||||
println!("========");
|
||||
symbol_table.debug();
|
||||
let mut state = State::new();
|
||||
let mut outputs = state.evaluate(reduced_ir, true);
|
||||
outputs.pop().unwrap()
|
||||
}
|
||||
|
||||
fn eval_assert(input: &str, expected: &str) {
|
||||
assert_eq!(evaluate_input(input), Ok(expected.to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_basic_eval() {
|
||||
eval_assert("1 + 2", "3");
|
||||
eval_assert("let mut a = 1; a = 2", "()");
|
||||
eval_assert("let mut a = 1; a = a + 2; a", "3");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn op_eval() {
|
||||
eval_assert("- 13", "-13");
|
||||
eval_assert("10 - 2", "8");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn function_eval() {
|
||||
eval_assert("fn oi(x) { x + 1 }; oi(4)", "5");
|
||||
eval_assert("fn oi(x) { x + 1 }; oi(1+2)", "4");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scopes() {
|
||||
let scope_ok = r#"
|
||||
let a = 20
|
||||
fn haha() {
|
||||
let something = 38
|
||||
let a = 10
|
||||
a
|
||||
}
|
||||
haha()
|
||||
"#;
|
||||
|
||||
eval_assert(scope_ok, "10");
|
||||
|
||||
let scope_ok = r#"
|
||||
let a = 20
|
||||
fn queque() {
|
||||
let a = 10
|
||||
a
|
||||
}
|
||||
a
|
||||
"#;
|
||||
eval_assert(scope_ok, "20");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn adt_output_1() {
|
||||
let source = r#"
|
||||
|
||||
type Option<T> = Some(T) | None
|
||||
let a = Option::None
|
||||
let b = Option::Some(10)
|
||||
(b, a)
|
||||
"#;
|
||||
eval_assert(source, "(Some(10), None)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic_if_statement() {
|
||||
let source = r#"
|
||||
let a = 10
|
||||
let b = 10
|
||||
if a == b then { 69 } else { 420 }
|
||||
"#;
|
||||
eval_assert(source, "69");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic_patterns_1() {
|
||||
let source =r#"
|
||||
let x = 10
|
||||
let a = if x is 10 then { 255 } else { 256 }
|
||||
let b = if 23 is 99 then { 255 } else { 256 }
|
||||
let c = if true is false then { 9 } else { 10 }
|
||||
let d = if "xxx" is "yyy" then { 20 } else { 30 }
|
||||
(a, b, c, d)
|
||||
"#;
|
||||
eval_assert(source, "(255, 256, 10, 30)");
|
||||
}
|
||||
|
||||
#[test_case("sanchez", "1")]
|
||||
#[test_case("mouri", "2")]
|
||||
#[test_case("hella", "3")]
|
||||
#[test_case("cyrus", "4")]
|
||||
fn basic_patterns_2(input: &str, expected: &str) {
|
||||
let mut source = format!(r#"let x = "{}""#, input);
|
||||
source.push_str(r#"
|
||||
if x {
|
||||
is "sanchez" then 1
|
||||
is "mouri" then 2
|
||||
is "hella" then 3
|
||||
is _ then 4
|
||||
}
|
||||
"#);
|
||||
eval_assert(&source, expected);
|
||||
}
|
||||
|
||||
#[test_case(r#"(45, "panda", false, 2.2)"#, r#""yes""#)]
|
||||
#[test_case(r#"(99, "panda", false, -2.45)"#, r#""maybe""#)]
|
||||
fn tuple_patterns(input: &str, expected: &str) {
|
||||
let mut source = format!("let x = {}", input);
|
||||
source.push_str(r#"
|
||||
if x {
|
||||
is (45, "pablo", _, 28.4) then "no"
|
||||
is (_, "panda", _, 2.2) then "yes"
|
||||
is _ then "maybe"
|
||||
}"#);
|
||||
|
||||
eval_assert(&source, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn if_is_patterns() {
|
||||
let source = r#"
|
||||
type Option<T> = Some(T) | None
|
||||
let q = "a string"
|
||||
let x = Option::Some(9); if x is Option::Some(q) then { q } else { 0 }"#;
|
||||
|
||||
eval_assert(source, "9");
|
||||
|
||||
let source = r#"
|
||||
type Option<T> = Some(T) | None
|
||||
let q = "a string"
|
||||
let outer = 2
|
||||
let x = Option::None; if x is Option::Some(q) then { q } else { -2 + outer }"#;
|
||||
eval_assert(source, "0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_if_matching() {
|
||||
let source = r#"
|
||||
type Option<T> = Some(T) | None
|
||||
let a = Option::None
|
||||
if a { is Option::None then 4, is Option::Some(x) then x }
|
||||
"#;
|
||||
eval_assert(source, "4");
|
||||
|
||||
let source = r#"
|
||||
type Option<T> = Some(T) | None
|
||||
let sara = Option::Some(99)
|
||||
if sara { is Option::None then 1 + 3, is Option::Some(x) then x }
|
||||
"#;
|
||||
eval_assert(source, "99");
|
||||
|
||||
let source = r#"
|
||||
let a = 10
|
||||
if a { is 10 then "x", is 4 then "y" }
|
||||
"#;
|
||||
eval_assert(source, "\"x\"");
|
||||
|
||||
let source = r#"
|
||||
let a = 10
|
||||
if a { is 15 then "x", is 10 then "y" }
|
||||
"#;
|
||||
eval_assert(source, "\"y\"");
|
||||
}
|
||||
|
||||
//TODO - I can probably cut down some of these
|
||||
#[test]
|
||||
fn string_pattern() {
|
||||
let source = r#"
|
||||
let a = "foo"
|
||||
if a { is "foo" then "x", is _ then "y" }
|
||||
"#;
|
||||
eval_assert(source, "\"x\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn boolean_pattern() {
|
||||
let source = r#"
|
||||
let a = true
|
||||
if a {
|
||||
is true then "x",
|
||||
is false then "y"
|
||||
}
|
||||
"#;
|
||||
eval_assert(source, "\"x\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn boolean_pattern_2() {
|
||||
let source = r#"
|
||||
let a = false
|
||||
if a { is true then "x", is false then "y" }
|
||||
"#;
|
||||
eval_assert(source, "\"y\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ignore_pattern() {
|
||||
let source = r#"
|
||||
type Option<T> = Some(T) | None
|
||||
if Option::Some(10) {
|
||||
is _ then "hella"
|
||||
}
|
||||
"#;
|
||||
eval_assert(source, "\"hella\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tuple_pattern() {
|
||||
let source = r#"
|
||||
if (1, 2) {
|
||||
is (1, x) then x,
|
||||
is _ then 99
|
||||
}
|
||||
"#;
|
||||
eval_assert(source, "2");
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn tuple_pattern_2() {
|
||||
let source = r#"
|
||||
if (1, 2) {
|
||||
is (10, x) then x,
|
||||
is (y, x) then x + y
|
||||
}
|
||||
"#;
|
||||
eval_assert(source, "3");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tuple_pattern_3() {
|
||||
let source = r#"
|
||||
if (1, 5) {
|
||||
is (10, x) then x,
|
||||
is (1, x) then x
|
||||
}
|
||||
"#;
|
||||
eval_assert(source, "5");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tuple_pattern_4() {
|
||||
let source = r#"
|
||||
if (1, 5) {
|
||||
is (10, x) then x,
|
||||
is (1, x) then x,
|
||||
}
|
||||
"#;
|
||||
eval_assert(source, "5");
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn prim_obj_pattern() {
|
||||
let source = r#"
|
||||
type Stuff = Mulch(Nat) | Jugs(Nat, String) | Mardok
|
||||
let a = Stuff::Mulch(20)
|
||||
let b = Stuff::Jugs(1, "haha")
|
||||
let c = Stuff::Mardok
|
||||
|
||||
let x = if a {
|
||||
is Stuff::Mulch(20) then "x",
|
||||
is _ then "ERR"
|
||||
}
|
||||
|
||||
let y = if b {
|
||||
is Stuff::Mulch(n) then "ERR",
|
||||
is Stuff::Jugs(2, _) then "ERR",
|
||||
is Stuff::Jugs(1, s) then s,
|
||||
is _ then "ERR",
|
||||
}
|
||||
|
||||
let z = if c {
|
||||
is Stuff::Jugs(_, _) then "ERR",
|
||||
is Stuff::Mardok then "NIGH",
|
||||
is _ then "ERR",
|
||||
}
|
||||
|
||||
(x, y, z)
|
||||
"#;
|
||||
eval_assert(source, r#"("x", "haha", "NIGH")"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic_lambda_evaluation_1() {
|
||||
let source = r#"
|
||||
let q = \(x, y) { x * y }
|
||||
let x = q(5, 2)
|
||||
let y = \(m, n, o) { m + n + o }(1,2,3)
|
||||
(x, y)
|
||||
"#;
|
||||
|
||||
eval_assert(source, r"(10, 6)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic_lambda_evaluation_2() {
|
||||
let source = r#"
|
||||
fn milta() {
|
||||
\(x) { x + 33 }
|
||||
}
|
||||
milta()(10)
|
||||
"#;
|
||||
|
||||
eval_assert(source, "43");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_all() {
|
||||
let source = r#"
|
||||
type Option<T> = Some(T) | None
|
||||
import Option::*
|
||||
let x = Some(9); if x is Some(q) then { q } else { 0 }"#;
|
||||
eval_assert(source, "9");
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
use std::rc::Rc;
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt;
|
||||
|
||||
use ena::unify::{UnifyKey, InPlaceUnificationTable, UnificationTable, EqUnifyValue};
|
||||
|
||||
@ -21,7 +22,31 @@ impl TypeData {
|
||||
}
|
||||
}
|
||||
|
||||
pub type TypeName = Rc<String>;
|
||||
//TODO need to hook this into the actual typechecking system somehow
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TypeId {
|
||||
local_name: Rc<String>
|
||||
}
|
||||
|
||||
impl TypeId {
|
||||
//TODO this is definitely incomplete
|
||||
pub fn lookup_name(name: &str) -> TypeId {
|
||||
TypeId {
|
||||
local_name: Rc::new(name.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn local_name(&self) -> &str {
|
||||
self.local_name.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for TypeId {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "TypeId:{}", self.local_name)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub struct TypeContext<'a> {
|
||||
variable_map: ScopeStack<'a, Rc<String>, Type>,
|
||||
|
@ -41,6 +41,7 @@ impl<'a, T, V, N> ScopeStack<'a, T, V, N> where T: Hash + Eq {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn lookup_with_scope(&self, key: &T) -> Option<(&V, Option<&N>)> where T: Hash + Eq {
|
||||
match (self.values.get(key), self.parent) {
|
||||
(None, None) => None,
|
||||
@ -49,7 +50,6 @@ impl<'a, T, V, N> ScopeStack<'a, T, V, N> where T: Hash + Eq {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn get_name(&self) -> Option<&N> {
|
||||
self.scope_name.as_ref()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user