cargo fmt
This commit is contained in:
parent
49c72e8930
commit
f26e24bba1
7 changed files with 596 additions and 538 deletions
54
src/expr.rs
54
src/expr.rs
|
@ -1,16 +1,13 @@
|
||||||
|
use std::{
|
||||||
use {
|
|
||||||
std::{
|
|
||||||
boxed::Box,
|
boxed::Box,
|
||||||
sync::{Arc, RwLock}
|
sync::{Arc, RwLock},
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum Statement {
|
pub enum Statement {
|
||||||
Assignment {
|
Assignment {
|
||||||
var_id: String,
|
var_id: String,
|
||||||
val_expr: LTExpr
|
val_expr: LTExpr,
|
||||||
},
|
},
|
||||||
LetAssign {
|
LetAssign {
|
||||||
typ: Option<TypeTag>,
|
typ: Option<TypeTag>,
|
||||||
|
@ -19,10 +16,10 @@ pub enum Statement {
|
||||||
},
|
},
|
||||||
WhileLoop {
|
WhileLoop {
|
||||||
condition: LTExpr,
|
condition: LTExpr,
|
||||||
body: Vec<Statement>
|
body: Vec<Statement>,
|
||||||
},
|
},
|
||||||
Return(LTExpr),
|
Return(LTExpr),
|
||||||
Expr(LTExpr)
|
Expr(LTExpr),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
|
@ -30,8 +27,8 @@ pub enum TypeError {
|
||||||
ParseError(laddertypes::parser::ParseError),
|
ParseError(laddertypes::parser::ParseError),
|
||||||
Mismatch {
|
Mismatch {
|
||||||
expected: laddertypes::TypeTerm,
|
expected: laddertypes::TypeTerm,
|
||||||
received: laddertypes::TypeTerm
|
received: laddertypes::TypeTerm,
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type TypeTag = Result<laddertypes::TypeTerm, TypeError>;
|
pub type TypeTag = Result<laddertypes::TypeTerm, TypeError>;
|
||||||
|
@ -40,7 +37,7 @@ pub type TypeTag = Result< laddertypes::TypeTerm, TypeError >;
|
||||||
pub enum LTExpr {
|
pub enum LTExpr {
|
||||||
Literal {
|
Literal {
|
||||||
typ: Option<TypeTag>,
|
typ: Option<TypeTag>,
|
||||||
val: tisc::VM_Word
|
val: tisc::VM_Word,
|
||||||
},
|
},
|
||||||
Symbol {
|
Symbol {
|
||||||
typ: Option<TypeTag>,
|
typ: Option<TypeTag>,
|
||||||
|
@ -49,44 +46,46 @@ pub enum LTExpr {
|
||||||
Application {
|
Application {
|
||||||
typ: Option<TypeTag>,
|
typ: Option<TypeTag>,
|
||||||
head: Box<LTExpr>,
|
head: Box<LTExpr>,
|
||||||
body: Vec<LTExpr>
|
body: Vec<LTExpr>,
|
||||||
},
|
},
|
||||||
Abstraction {
|
Abstraction {
|
||||||
args: Vec<(String, Option<TypeTag>)>,
|
args: Vec<(String, Option<TypeTag>)>,
|
||||||
body: Box<LTExpr>
|
body: Box<LTExpr>,
|
||||||
},
|
},
|
||||||
Branch {
|
Branch {
|
||||||
condition: Box<LTExpr>,
|
condition: Box<LTExpr>,
|
||||||
if_expr: Box<LTExpr>,
|
if_expr: Box<LTExpr>,
|
||||||
else_expr: Box<LTExpr>
|
else_expr: Box<LTExpr>,
|
||||||
},
|
},
|
||||||
Block {
|
Block {
|
||||||
statements: Vec<Statement>
|
statements: Vec<Statement>,
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LTExpr {
|
impl LTExpr {
|
||||||
pub fn symbol(str: &str) -> Self {
|
pub fn symbol(str: &str) -> Self {
|
||||||
LTExpr::Symbol {
|
LTExpr::Symbol {
|
||||||
typ: None, //typectx.write().unwrap().parse("<Ref memory::Word>~Symbol~<Seq Char>").expect("parse typeterm"),
|
typ: None, //typectx.write().unwrap().parse("<Ref memory::Word>~Symbol~<Seq Char>").expect("parse typeterm"),
|
||||||
symbol: String::from(str)
|
symbol: String::from(str),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lit_uint(val: u64) -> Self {
|
pub fn lit_uint(val: u64) -> Self {
|
||||||
LTExpr::Literal {
|
LTExpr::Literal {
|
||||||
typ: None, //typectx.write().unwrap().parse("ℤ_2^64~machine::UInt64~machine::Word").expect("parse typeterm"),
|
typ: None, //typectx.write().unwrap().parse("ℤ_2^64~machine::UInt64~machine::Word").expect("parse typeterm"),
|
||||||
val: val as tisc::VM_Word
|
val: val as tisc::VM_Word,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn abstraction(args: Vec<(&str, &str)>, body: LTExpr) -> LTExpr {
|
pub fn abstraction(args: Vec<(&str, &str)>, body: LTExpr) -> LTExpr {
|
||||||
LTExpr::Abstraction {
|
LTExpr::Abstraction {
|
||||||
args: args.into_iter().map(|(arg_name, arg_type)|
|
args: args
|
||||||
( arg_name.into(), None )
|
.into_iter()
|
||||||
//typectx.write().unwrap().parse(t).expect("parse typeterm")
|
.map(
|
||||||
).collect(),
|
|(arg_name, arg_type)| (arg_name.into(), None), //typectx.write().unwrap().parse(t).expect("parse typeterm")
|
||||||
body: Box::new(body)
|
)
|
||||||
|
.collect(),
|
||||||
|
body: Box::new(body),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,14 +93,12 @@ impl LTExpr {
|
||||||
LTExpr::Application {
|
LTExpr::Application {
|
||||||
typ: None,
|
typ: None,
|
||||||
head: Box::new(head),
|
head: Box::new(head),
|
||||||
body: body
|
body: body,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn block(body: Vec<Statement>) -> Self {
|
pub fn block(body: Vec<Statement>) -> Self {
|
||||||
LTExpr::Block {
|
LTExpr::Block { statements: body }
|
||||||
statements: body
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,7 +106,7 @@ impl Statement {
|
||||||
pub fn while_loop(cond: LTExpr, body: Vec<Statement>) -> Self {
|
pub fn while_loop(cond: LTExpr, body: Vec<Statement>) -> Self {
|
||||||
Statement::WhileLoop {
|
Statement::WhileLoop {
|
||||||
condition: cond,
|
condition: cond,
|
||||||
body
|
body,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -156,4 +153,3 @@ impl LTExpr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
106
src/lexer.rs
106
src/lexer.rs
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||||
pub enum LTIRToken {
|
pub enum LTIRToken {
|
||||||
Symbol(String),
|
Symbol(String),
|
||||||
|
@ -8,7 +7,6 @@ pub enum LTIRToken {
|
||||||
// SingleQuote(String),
|
// SingleQuote(String),
|
||||||
// DoubleQuote(String),
|
// DoubleQuote(String),
|
||||||
// TripleQuote(String),
|
// TripleQuote(String),
|
||||||
|
|
||||||
Lambda,
|
Lambda,
|
||||||
LambdaBody,
|
LambdaBody,
|
||||||
AssignType(String),
|
AssignType(String),
|
||||||
|
@ -25,7 +23,7 @@ pub enum LTIRToken {
|
||||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||||
pub enum LexError {
|
pub enum LexError {
|
||||||
InvalidDigit,
|
InvalidDigit,
|
||||||
InvalidChar
|
InvalidChar,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||||
|
@ -34,7 +32,7 @@ pub enum LexerState {
|
||||||
TypeTerm(String),
|
TypeTerm(String),
|
||||||
Sym(String),
|
Sym(String),
|
||||||
Num(i64),
|
Num(i64),
|
||||||
Char( Option<char> )
|
Char(Option<char>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LexerState {
|
impl LexerState {
|
||||||
|
@ -44,20 +42,21 @@ impl LexerState {
|
||||||
LexerState::TypeTerm(s) => Some(LTIRToken::AssignType(s)),
|
LexerState::TypeTerm(s) => Some(LTIRToken::AssignType(s)),
|
||||||
LexerState::Sym(s) => Some(LTIRToken::Symbol(s)),
|
LexerState::Sym(s) => Some(LTIRToken::Symbol(s)),
|
||||||
LexerState::Num(n) => Some(LTIRToken::Num(n)),
|
LexerState::Num(n) => Some(LTIRToken::Num(n)),
|
||||||
LexerState::Char(c) => Some(LTIRToken::Char(c?))
|
LexerState::Char(c) => Some(LTIRToken::Char(c?)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub struct LTIRLexer<It>
|
pub struct LTIRLexer<It>
|
||||||
where It: std::iter::Iterator<Item = char>
|
where
|
||||||
|
It: std::iter::Iterator<Item = char>,
|
||||||
{
|
{
|
||||||
chars: std::iter::Peekable<It>,
|
chars: std::iter::Peekable<It>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<It> LTIRLexer<It>
|
impl<It> LTIRLexer<It>
|
||||||
where It: Iterator<Item = char>
|
where
|
||||||
|
It: Iterator<Item = char>,
|
||||||
{
|
{
|
||||||
pub fn into_inner(self) -> std::iter::Peekable<It> {
|
pub fn into_inner(self) -> std::iter::Peekable<It> {
|
||||||
self.chars
|
self.chars
|
||||||
|
@ -65,17 +64,19 @@ where It: Iterator<Item = char>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<It> From<It> for LTIRLexer<It>
|
impl<It> From<It> for LTIRLexer<It>
|
||||||
where It: Iterator<Item = char>
|
where
|
||||||
|
It: Iterator<Item = char>,
|
||||||
{
|
{
|
||||||
fn from(chars: It) -> Self {
|
fn from(chars: It) -> Self {
|
||||||
LTIRLexer {
|
LTIRLexer {
|
||||||
chars: chars.peekable()
|
chars: chars.peekable(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<It> Iterator for LTIRLexer<It>
|
impl<It> Iterator for LTIRLexer<It>
|
||||||
where It: Iterator<Item = char>
|
where
|
||||||
|
It: Iterator<Item = char>,
|
||||||
{
|
{
|
||||||
type Item = Result<LTIRToken, LexError>;
|
type Item = Result<LTIRToken, LexError>;
|
||||||
|
|
||||||
|
@ -84,23 +85,48 @@ where It: Iterator<Item = char>
|
||||||
|
|
||||||
while let Some(c) = self.chars.peek() {
|
while let Some(c) = self.chars.peek() {
|
||||||
match &mut state {
|
match &mut state {
|
||||||
|
|
||||||
// determine token type
|
// determine token type
|
||||||
LexerState::Any => {
|
LexerState::Any => match c {
|
||||||
match c {
|
'λ' => {
|
||||||
'λ' => { self.chars.next(); return Some(Ok(LTIRToken::Lambda)); },
|
self.chars.next();
|
||||||
'.' => { self.chars.next(); return Some(Ok(LTIRToken::LambdaBody)); },
|
return Some(Ok(LTIRToken::Lambda));
|
||||||
'(' => { self.chars.next(); return Some(Ok(LTIRToken::ExprOpen)); },
|
}
|
||||||
')' => { self.chars.next(); return Some(Ok(LTIRToken::ExprClose)); },
|
'.' => {
|
||||||
'{' => { self.chars.next(); return Some(Ok(LTIRToken::BlockOpen)); },
|
self.chars.next();
|
||||||
'}' => { self.chars.next(); return Some(Ok(LTIRToken::BlockClose)); },
|
return Some(Ok(LTIRToken::LambdaBody));
|
||||||
|
}
|
||||||
|
'(' => {
|
||||||
|
self.chars.next();
|
||||||
|
return Some(Ok(LTIRToken::ExprOpen));
|
||||||
|
}
|
||||||
|
')' => {
|
||||||
|
self.chars.next();
|
||||||
|
return Some(Ok(LTIRToken::ExprClose));
|
||||||
|
}
|
||||||
|
'{' => {
|
||||||
|
self.chars.next();
|
||||||
|
return Some(Ok(LTIRToken::BlockOpen));
|
||||||
|
}
|
||||||
|
'}' => {
|
||||||
|
self.chars.next();
|
||||||
|
return Some(Ok(LTIRToken::BlockClose));
|
||||||
|
}
|
||||||
':' => {
|
':' => {
|
||||||
self.chars.next();
|
self.chars.next();
|
||||||
state = LexerState::TypeTerm(String::new());
|
state = LexerState::TypeTerm(String::new());
|
||||||
},
|
}
|
||||||
'=' => { self.chars.next(); return Some(Ok(LTIRToken::AssignValue)); },
|
'=' => {
|
||||||
';' => { self.chars.next(); return Some(Ok(LTIRToken::StatementSep)); },
|
self.chars.next();
|
||||||
'\'' => { self.chars.next(); state = LexerState::Char(None); },
|
return Some(Ok(LTIRToken::AssignValue));
|
||||||
|
}
|
||||||
|
';' => {
|
||||||
|
self.chars.next();
|
||||||
|
return Some(Ok(LTIRToken::StatementSep));
|
||||||
|
}
|
||||||
|
'\'' => {
|
||||||
|
self.chars.next();
|
||||||
|
state = LexerState::Char(None);
|
||||||
|
}
|
||||||
c => {
|
c => {
|
||||||
if c.is_whitespace() {
|
if c.is_whitespace() {
|
||||||
self.chars.next();
|
self.chars.next();
|
||||||
|
@ -110,14 +136,11 @@ where It: Iterator<Item = char>
|
||||||
state = LexerState::Sym(String::new());
|
state = LexerState::Sym(String::new());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
|
||||||
|
|
||||||
LexerState::Char(val) => {
|
LexerState::Char(val) => {
|
||||||
*val = Some(
|
*val = Some(match self.chars.next() {
|
||||||
match self.chars.next() {
|
Some('\\') => match self.chars.next() {
|
||||||
Some('\\') => {
|
|
||||||
match self.chars.next() {
|
|
||||||
Some('0') => '\0',
|
Some('0') => '\0',
|
||||||
Some('n') => '\n',
|
Some('n') => '\n',
|
||||||
Some('t') => '\t',
|
Some('t') => '\t',
|
||||||
|
@ -125,8 +148,7 @@ where It: Iterator<Item = char>
|
||||||
None => {
|
None => {
|
||||||
return Some(Err(LexError::InvalidChar));
|
return Some(Err(LexError::InvalidChar));
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
|
||||||
Some(c) => c,
|
Some(c) => c,
|
||||||
None => {
|
None => {
|
||||||
return Some(Err(LexError::InvalidChar));
|
return Some(Err(LexError::InvalidChar));
|
||||||
|
@ -148,7 +170,7 @@ where It: Iterator<Item = char>
|
||||||
LexerState::TypeTerm(s) => {
|
LexerState::TypeTerm(s) => {
|
||||||
if *c == '=' || *c == '.' {
|
if *c == '=' || *c == '.' {
|
||||||
if let Some(token) = state.clone().into_token() {
|
if let Some(token) = state.clone().into_token() {
|
||||||
return Some(Ok(token))
|
return Some(Ok(token));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if let Some(c) = self.chars.next() {
|
if let Some(c) = self.chars.next() {
|
||||||
|
@ -158,12 +180,15 @@ where It: Iterator<Item = char>
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
|
|
||||||
if c.is_whitespace()
|
if c.is_whitespace()
|
||||||
|| *c == '(' || *c == ')'
|
|| *c == '('
|
||||||
|| *c == '{' || *c == '}'
|
|| *c == ')'
|
||||||
|| *c == ';' || *c == '='
|
|| *c == '{'
|
||||||
|| *c == ':' || *c == '.'
|
|| *c == '}'
|
||||||
|
|| *c == ';'
|
||||||
|
|| *c == '='
|
||||||
|
|| *c == ':'
|
||||||
|
|| *c == '.'
|
||||||
{
|
{
|
||||||
// finish the current token
|
// finish the current token
|
||||||
|
|
||||||
|
@ -215,7 +240,8 @@ mod tests {
|
||||||
λx:ℝ
|
λx:ℝ
|
||||||
.λy:ℝ
|
.λy:ℝ
|
||||||
.sqrt (+ (* x x) (* y y));
|
.sqrt (+ (* x x) (* y y));
|
||||||
".chars()
|
"
|
||||||
|
.chars(),
|
||||||
);
|
);
|
||||||
|
|
||||||
for token in lexer {
|
for token in lexer {
|
||||||
|
@ -223,5 +249,3 @@ mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
74
src/main.rs
74
src/main.rs
|
@ -1,31 +1,34 @@
|
||||||
use {
|
use {
|
||||||
std::{boxed::{Box}, ops::Deref},
|
|
||||||
std::collections::HashMap,
|
std::collections::HashMap,
|
||||||
std::sync::{Arc, RwLock},
|
std::sync::{Arc, RwLock},
|
||||||
|
std::{boxed::Box, ops::Deref},
|
||||||
};
|
};
|
||||||
|
|
||||||
mod expr;
|
mod expr;
|
||||||
mod symbols;
|
|
||||||
mod procedure_compiler;
|
|
||||||
mod runtime;
|
|
||||||
mod lexer;
|
mod lexer;
|
||||||
mod parser;
|
mod parser;
|
||||||
|
mod procedure_compiler;
|
||||||
|
mod runtime;
|
||||||
|
mod symbols;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
expr::{LTExpr, Statement},
|
expr::{LTExpr, Statement},
|
||||||
symbols::{Scope},
|
procedure_compiler::ProcedureCompiler,
|
||||||
procedure_compiler::ProcedureCompiler
|
symbols::Scope,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn compile(scope: &Arc<RwLock<Scope>>, name: &str, source: &str) -> Vec< tisc::assembler::AssemblyWord > {
|
fn compile(
|
||||||
|
scope: &Arc<RwLock<Scope>>,
|
||||||
|
name: &str,
|
||||||
|
source: &str,
|
||||||
|
) -> Vec<tisc::assembler::AssemblyWord> {
|
||||||
ProcedureCompiler::new(scope)
|
ProcedureCompiler::new(scope)
|
||||||
.compile(
|
.compile(
|
||||||
&parser::parse_expr(
|
&parser::parse_expr(
|
||||||
&scope.read().unwrap().typectx,
|
&scope.read().unwrap().typectx,
|
||||||
&mut lexer::LTIRLexer::from(
|
&mut lexer::LTIRLexer::from(source.chars().peekable()).peekable(),
|
||||||
source.chars().peekable()
|
)
|
||||||
).peekable()
|
.expect("syntax error"),
|
||||||
).expect("syntax error")
|
|
||||||
)
|
)
|
||||||
.into_asm(&name.into())
|
.into_asm(&name.into())
|
||||||
}
|
}
|
||||||
|
@ -41,27 +44,28 @@ fn main() {
|
||||||
|
|
||||||
/* define type of the symbol
|
/* define type of the symbol
|
||||||
*/
|
*/
|
||||||
main_scope.write().unwrap()
|
main_scope.write().unwrap().declare_static_parse(
|
||||||
.declare_static_parse(
|
|
||||||
"hello-string",
|
"hello-string",
|
||||||
"<Seq Char
|
"<Seq Char
|
||||||
~Ascii
|
~Ascii
|
||||||
~machine::Word>
|
~machine::Word>
|
||||||
~<NullTerminatedSeq machine::Word>"
|
~<NullTerminatedSeq machine::Word>",
|
||||||
);
|
);
|
||||||
|
|
||||||
main_scope.write().unwrap()
|
main_scope.write().unwrap().declare_static_parse(
|
||||||
.declare_static_parse(
|
|
||||||
"pfxstr",
|
"pfxstr",
|
||||||
"<Seq Char
|
"<Seq Char
|
||||||
~Ascii
|
~Ascii
|
||||||
~machine::Word>
|
~machine::Word>
|
||||||
~<LengthPrefixedSeq machine::Word>"
|
~<LengthPrefixedSeq machine::Word>",
|
||||||
);
|
);
|
||||||
|
|
||||||
/* link assembly-program to symbols
|
/* link assembly-program to symbols
|
||||||
*/
|
*/
|
||||||
linker.add_procedure("main", compile(&main_scope,
|
linker.add_procedure(
|
||||||
|
"main",
|
||||||
|
compile(
|
||||||
|
&main_scope,
|
||||||
"main",
|
"main",
|
||||||
"{
|
"{
|
||||||
let print-nullterm =
|
let print-nullterm =
|
||||||
|
@ -137,23 +141,37 @@ fn main() {
|
||||||
};
|
};
|
||||||
|
|
||||||
hello;
|
hello;
|
||||||
}"
|
}",
|
||||||
));
|
),
|
||||||
|
);
|
||||||
|
|
||||||
linker.add_static("hello-string",
|
linker.add_static(
|
||||||
|
"hello-string",
|
||||||
"Hallo Welt!\n\0"
|
"Hallo Welt!\n\0"
|
||||||
.chars()
|
.chars()
|
||||||
.map(|c| (c as u8) as tisc::VM_Word)
|
.map(|c| (c as u8) as tisc::VM_Word)
|
||||||
.collect());
|
.collect(),
|
||||||
|
|
||||||
linker.add_static("pfxstr",
|
|
||||||
vec![ 3, 'a' as tisc::VM_Word, 'b' as tisc::VM_Word, 'c' as tisc::VM_Word, 'd' as tisc::VM_Word ]
|
|
||||||
);
|
);
|
||||||
|
|
||||||
let main_addr = linker.get_link_addr(&"main".into()).expect("'main' not linked");
|
linker.add_static(
|
||||||
|
"pfxstr",
|
||||||
|
vec![
|
||||||
|
3,
|
||||||
|
'a' as tisc::VM_Word,
|
||||||
|
'b' as tisc::VM_Word,
|
||||||
|
'c' as tisc::VM_Word,
|
||||||
|
'd' as tisc::VM_Word,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
let main_addr = linker
|
||||||
|
.get_link_addr(&"main".into())
|
||||||
|
.expect("'main' not linked");
|
||||||
vm.load(linker.link_total().expect("could not link"));
|
vm.load(linker.link_total().expect("could not link"));
|
||||||
vm.execute(main_addr);
|
vm.execute(main_addr);
|
||||||
|
|
||||||
eprintln!("\n====\nVM execution finished\ndatastack = {:?}\n====", vm.data_stack);
|
eprintln!(
|
||||||
|
"\n====\nVM execution finished\ndatastack = {:?}\n====",
|
||||||
|
vm.data_stack
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
143
src/parser.rs
143
src/parser.rs
|
@ -1,12 +1,12 @@
|
||||||
use {
|
use {
|
||||||
|
crate::{
|
||||||
|
expr::{LTExpr, Statement, TypeError, TypeTag},
|
||||||
|
lexer::{LTIRLexer, LTIRToken, LexError},
|
||||||
|
},
|
||||||
std::{
|
std::{
|
||||||
iter::Peekable,
|
iter::Peekable,
|
||||||
sync::{Arc, RwLock}
|
sync::{Arc, RwLock},
|
||||||
},
|
},
|
||||||
crate::{
|
|
||||||
lexer::{LTIRLexer, LTIRToken, LexError},
|
|
||||||
expr::{LTExpr, Statement, TypeTag, TypeError}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
|
@ -14,14 +14,15 @@ pub enum ParseError {
|
||||||
LexError(LexError),
|
LexError(LexError),
|
||||||
UnexpectedClose,
|
UnexpectedClose,
|
||||||
UnexpectedEnd,
|
UnexpectedEnd,
|
||||||
UnexpectedToken
|
UnexpectedToken,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_expect<It>(
|
pub fn parse_expect<It>(
|
||||||
tokens: &mut Peekable<LTIRLexer<It>>,
|
tokens: &mut Peekable<LTIRLexer<It>>,
|
||||||
expected_token: LTIRToken
|
expected_token: LTIRToken,
|
||||||
) -> Result<(), ParseError>
|
) -> Result<(), ParseError>
|
||||||
where It: Iterator<Item = char>
|
where
|
||||||
|
It: Iterator<Item = char>,
|
||||||
{
|
{
|
||||||
match tokens.next() {
|
match tokens.next() {
|
||||||
Some(Ok(t)) => {
|
Some(Ok(t)) => {
|
||||||
|
@ -30,16 +31,15 @@ where It: Iterator<Item = char>
|
||||||
} else {
|
} else {
|
||||||
Err(ParseError::UnexpectedToken)
|
Err(ParseError::UnexpectedToken)
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Some(Err(err)) => Err(ParseError::LexError(err)),
|
Some(Err(err)) => Err(ParseError::LexError(err)),
|
||||||
None => Err(ParseError::UnexpectedEnd)
|
None => Err(ParseError::UnexpectedEnd),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_symbol<It>(
|
pub fn parse_symbol<It>(tokens: &mut Peekable<LTIRLexer<It>>) -> Result<String, ParseError>
|
||||||
tokens: &mut Peekable<LTIRLexer<It>>
|
where
|
||||||
) -> Result< String, ParseError >
|
It: Iterator<Item = char>,
|
||||||
where It: Iterator<Item = char>
|
|
||||||
{
|
{
|
||||||
match tokens.next() {
|
match tokens.next() {
|
||||||
Some(Ok(LTIRToken::Symbol(name))) => Ok(name),
|
Some(Ok(LTIRToken::Symbol(name))) => Ok(name),
|
||||||
|
@ -51,24 +51,21 @@ where It: Iterator<Item = char>
|
||||||
|
|
||||||
pub fn parse_type_tag<It>(
|
pub fn parse_type_tag<It>(
|
||||||
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
|
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
|
||||||
tokens: &mut Peekable<LTIRLexer<It>>
|
tokens: &mut Peekable<LTIRLexer<It>>,
|
||||||
) -> Option<TypeTag>
|
) -> Option<TypeTag>
|
||||||
where It: Iterator<Item = char>
|
where
|
||||||
|
It: Iterator<Item = char>,
|
||||||
{
|
{
|
||||||
if let Some(peektok) = tokens.peek().clone() {
|
if let Some(peektok) = tokens.peek().clone() {
|
||||||
match peektok.clone() {
|
match peektok.clone() {
|
||||||
Ok(LTIRToken::AssignType(typeterm_str)) => {
|
Ok(LTIRToken::AssignType(typeterm_str)) => {
|
||||||
tokens.next();
|
tokens.next();
|
||||||
match typectx.write().unwrap().parse(typeterm_str.as_str()) {
|
match typectx.write().unwrap().parse(typeterm_str.as_str()) {
|
||||||
Ok(typeterm) => {
|
Ok(typeterm) => Some(Ok(typeterm)),
|
||||||
Some(Ok(typeterm))
|
Err(parse_error) => Some(Err(TypeError::ParseError(parse_error))),
|
||||||
}
|
|
||||||
Err(parse_error) => {
|
|
||||||
Some(Err(TypeError::ParseError(parse_error)))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
_ => None,
|
||||||
_ => None
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -77,9 +74,10 @@ where It: Iterator<Item = char>
|
||||||
|
|
||||||
pub fn parse_statement<It>(
|
pub fn parse_statement<It>(
|
||||||
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
|
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
|
||||||
tokens: &mut Peekable<LTIRLexer<It>>
|
tokens: &mut Peekable<LTIRLexer<It>>,
|
||||||
) -> Result<crate::expr::Statement, ParseError>
|
) -> Result<crate::expr::Statement, ParseError>
|
||||||
where It: Iterator<Item = char>
|
where
|
||||||
|
It: Iterator<Item = char>,
|
||||||
{
|
{
|
||||||
if let Some(peektok) = tokens.peek() {
|
if let Some(peektok) = tokens.peek() {
|
||||||
match peektok {
|
match peektok {
|
||||||
|
@ -94,7 +92,7 @@ where It: Iterator<Item = char>
|
||||||
|
|
||||||
Ok(Statement::Assignment {
|
Ok(Statement::Assignment {
|
||||||
var_id: name,
|
var_id: name,
|
||||||
val_expr
|
val_expr,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
"let" => {
|
"let" => {
|
||||||
|
@ -108,7 +106,7 @@ where It: Iterator<Item = char>
|
||||||
Ok(Statement::LetAssign {
|
Ok(Statement::LetAssign {
|
||||||
typ,
|
typ,
|
||||||
var_id: name,
|
var_id: name,
|
||||||
val_expr
|
val_expr,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
"while" => {
|
"while" => {
|
||||||
|
@ -118,7 +116,7 @@ where It: Iterator<Item = char>
|
||||||
let _ = parse_expect(tokens, LTIRToken::ExprClose)?;
|
let _ = parse_expect(tokens, LTIRToken::ExprClose)?;
|
||||||
Ok(Statement::WhileLoop {
|
Ok(Statement::WhileLoop {
|
||||||
condition: cond,
|
condition: cond,
|
||||||
body: parse_block(typectx, tokens)?
|
body: parse_block(typectx, tokens)?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
"return" => {
|
"return" => {
|
||||||
|
@ -138,8 +136,8 @@ where It: Iterator<Item = char>
|
||||||
let expr = parse_expr(typectx, tokens)?;
|
let expr = parse_expr(typectx, tokens)?;
|
||||||
let _ = parse_expect(tokens, LTIRToken::StatementSep)?;
|
let _ = parse_expect(tokens, LTIRToken::StatementSep)?;
|
||||||
Ok(Statement::Expr(expr))
|
Ok(Statement::Expr(expr))
|
||||||
},
|
}
|
||||||
Err(err) => Err(ParseError::LexError(err.clone()))
|
Err(err) => Err(ParseError::LexError(err.clone())),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Err(ParseError::UnexpectedEnd)
|
Err(ParseError::UnexpectedEnd)
|
||||||
|
@ -148,9 +146,10 @@ where It: Iterator<Item = char>
|
||||||
|
|
||||||
pub fn parse_block<It>(
|
pub fn parse_block<It>(
|
||||||
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
|
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
|
||||||
tokens: &mut Peekable<LTIRLexer<It>>
|
tokens: &mut Peekable<LTIRLexer<It>>,
|
||||||
) -> Result<Vec<Statement>, ParseError>
|
) -> Result<Vec<Statement>, ParseError>
|
||||||
where It: Iterator<Item = char>
|
where
|
||||||
|
It: Iterator<Item = char>,
|
||||||
{
|
{
|
||||||
let _ = parse_expect(tokens, LTIRToken::BlockOpen)?;
|
let _ = parse_expect(tokens, LTIRToken::BlockOpen)?;
|
||||||
|
|
||||||
|
@ -159,10 +158,14 @@ where It: Iterator<Item = char>
|
||||||
match peektok {
|
match peektok {
|
||||||
Ok(LTIRToken::BlockClose) => {
|
Ok(LTIRToken::BlockClose) => {
|
||||||
tokens.next();
|
tokens.next();
|
||||||
return Ok(statements)
|
return Ok(statements);
|
||||||
|
}
|
||||||
|
Ok(_) => {
|
||||||
|
statements.push(parse_statement(typectx, tokens)?);
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
return Err(ParseError::LexError(err.clone()));
|
||||||
}
|
}
|
||||||
Ok(_) => { statements.push( parse_statement(typectx, tokens)? ); }
|
|
||||||
Err(err) => { return Err(ParseError::LexError(err.clone())); }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -170,37 +173,27 @@ where It: Iterator<Item = char>
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_atom<It>(
|
pub fn parse_atom<It>(
|
||||||
tokens: &mut Peekable<LTIRLexer<It>>
|
tokens: &mut Peekable<LTIRLexer<It>>,
|
||||||
) -> Result<crate::expr::LTExpr, ParseError>
|
) -> Result<crate::expr::LTExpr, ParseError>
|
||||||
where It: Iterator<Item = char>
|
where
|
||||||
|
It: Iterator<Item = char>,
|
||||||
{
|
{
|
||||||
match tokens.next() {
|
match tokens.next() {
|
||||||
Some(Ok(LTIRToken::Symbol(sym))) => {
|
Some(Ok(LTIRToken::Symbol(sym))) => Ok(LTExpr::symbol(sym.as_str())),
|
||||||
Ok(LTExpr::symbol(sym.as_str()))
|
Some(Ok(LTIRToken::Char(c))) => Ok(LTExpr::lit_uint(c as u64)),
|
||||||
}
|
Some(Ok(LTIRToken::Num(n))) => Ok(LTExpr::lit_uint(n as u64)),
|
||||||
Some(Ok(LTIRToken::Char(c))) => {
|
Some(Ok(_)) => Err(ParseError::UnexpectedToken),
|
||||||
Ok(LTExpr::lit_uint(c as u64))
|
Some(Err(err)) => Err(ParseError::LexError(err)),
|
||||||
}
|
None => Err(ParseError::UnexpectedEnd),
|
||||||
Some(Ok(LTIRToken::Num(n))) => {
|
|
||||||
Ok(LTExpr::lit_uint(n as u64))
|
|
||||||
}
|
|
||||||
Some(Ok(_)) => {
|
|
||||||
Err(ParseError::UnexpectedToken)
|
|
||||||
}
|
|
||||||
Some(Err(err)) => {
|
|
||||||
Err(ParseError::LexError(err))
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
Err(ParseError::UnexpectedEnd)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_expr<It>(
|
pub fn parse_expr<It>(
|
||||||
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
|
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
|
||||||
tokens: &mut Peekable<LTIRLexer<It>>
|
tokens: &mut Peekable<LTIRLexer<It>>,
|
||||||
) -> Result<crate::expr::LTExpr, ParseError>
|
) -> Result<crate::expr::LTExpr, ParseError>
|
||||||
where It: Iterator<Item = char>
|
where
|
||||||
|
It: Iterator<Item = char>,
|
||||||
{
|
{
|
||||||
let mut children = Vec::new();
|
let mut children = Vec::new();
|
||||||
|
|
||||||
|
@ -212,10 +205,7 @@ where It: Iterator<Item = char>
|
||||||
|
|
||||||
let mut args = Vec::new();
|
let mut args = Vec::new();
|
||||||
while let Some(Ok(LTIRToken::Symbol(_))) = tokens.peek() {
|
while let Some(Ok(LTIRToken::Symbol(_))) = tokens.peek() {
|
||||||
args.push((
|
args.push((parse_symbol(tokens)?, parse_type_tag(typectx, tokens)));
|
||||||
parse_symbol(tokens)?,
|
|
||||||
parse_type_tag(typectx, tokens)
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let _ = parse_expect(tokens, LTIRToken::LambdaBody);
|
let _ = parse_expect(tokens, LTIRToken::LambdaBody);
|
||||||
|
@ -223,7 +213,7 @@ where It: Iterator<Item = char>
|
||||||
|
|
||||||
return Ok(LTExpr::Abstraction {
|
return Ok(LTExpr::Abstraction {
|
||||||
args,
|
args,
|
||||||
body: Box::new(body)
|
body: Box::new(body),
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
return Err(ParseError::UnexpectedToken);
|
return Err(ParseError::UnexpectedToken);
|
||||||
|
@ -241,15 +231,20 @@ where It: Iterator<Item = char>
|
||||||
}
|
}
|
||||||
children.push(parse_expr(typectx, tokens)?);
|
children.push(parse_expr(typectx, tokens)?);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Ok(LTIRToken::ExprClose) => { break; }
|
Ok(LTIRToken::ExprClose) => {
|
||||||
|
break;
|
||||||
|
}
|
||||||
Ok(LTIRToken::BlockOpen) => {
|
Ok(LTIRToken::BlockOpen) => {
|
||||||
children.push(LTExpr::block(parse_block(typectx, tokens)?));
|
children.push(LTExpr::block(parse_block(typectx, tokens)?));
|
||||||
}
|
}
|
||||||
Ok(LTIRToken::BlockClose) => { break; }
|
Ok(LTIRToken::BlockClose) => {
|
||||||
Ok(LTIRToken::StatementSep) => { break; }
|
break;
|
||||||
Ok(LTIRToken::Symbol(name)) => {
|
}
|
||||||
match name.as_str() {
|
Ok(LTIRToken::StatementSep) => {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Ok(LTIRToken::Symbol(name)) => match name.as_str() {
|
||||||
"if" => {
|
"if" => {
|
||||||
tokens.next();
|
tokens.next();
|
||||||
let _ = parse_expect(tokens, LTIRToken::ExprOpen)?;
|
let _ = parse_expect(tokens, LTIRToken::ExprOpen)?;
|
||||||
|
@ -270,16 +265,19 @@ where It: Iterator<Item = char>
|
||||||
children.push(LTExpr::Branch {
|
children.push(LTExpr::Branch {
|
||||||
condition: Box::new(cond),
|
condition: Box::new(cond),
|
||||||
if_expr: Box::new(if_expr),
|
if_expr: Box::new(if_expr),
|
||||||
else_expr: Box::new(else_expr)
|
else_expr: Box::new(else_expr),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
name => {
|
name => {
|
||||||
children.push(parse_atom(tokens)?);
|
children.push(parse_atom(tokens)?);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
Ok(atom) => {
|
||||||
|
children.push(parse_atom(tokens)?);
|
||||||
}
|
}
|
||||||
|
Err(err) => {
|
||||||
|
return Err(ParseError::LexError(err.clone()));
|
||||||
}
|
}
|
||||||
Ok(atom) => { children.push(parse_atom(tokens)?); }
|
|
||||||
Err(err) => { return Err(ParseError::LexError(err.clone())); }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -288,10 +286,9 @@ where It: Iterator<Item = char>
|
||||||
Ok(LTExpr::Application {
|
Ok(LTExpr::Application {
|
||||||
typ: None,
|
typ: None,
|
||||||
head: Box::new(head),
|
head: Box::new(head),
|
||||||
body: children
|
body: children,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
Err(ParseError::UnexpectedEnd)
|
Err(ParseError::UnexpectedEnd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,13 @@
|
||||||
|
|
||||||
use {
|
use {
|
||||||
std::{
|
|
||||||
sync::{Arc, RwLock},
|
|
||||||
ops::Deref,
|
|
||||||
},
|
|
||||||
tisc::{
|
|
||||||
assembler::AssemblyWord,
|
|
||||||
linker::LinkAddr
|
|
||||||
},
|
|
||||||
crate::{
|
crate::{
|
||||||
expr::{LTExpr, Statement},
|
expr::{LTExpr, Statement},
|
||||||
symbols::{Scope, SymbolDef}
|
symbols::{Scope, SymbolDef},
|
||||||
}
|
},
|
||||||
|
std::{
|
||||||
|
ops::Deref,
|
||||||
|
sync::{Arc, RwLock},
|
||||||
|
},
|
||||||
|
tisc::{assembler::AssemblyWord, linker::LinkAddr},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct ProcedureCompiler {
|
pub struct ProcedureCompiler {
|
||||||
|
@ -36,22 +32,26 @@ impl ProcedureCompiler {
|
||||||
|
|
||||||
let body = self.asm.build();
|
let body = self.asm.build();
|
||||||
self.linker.add_procedure("__procedure_body__", body);
|
self.linker.add_procedure("__procedure_body__", body);
|
||||||
let body_addr = self.linker.get_link_addr(&"__procedure_body__".into()).unwrap();
|
let body_addr = self
|
||||||
let subroutines = self.linker.link_relative(&"__subroutines__".into()).expect("link error");
|
.linker
|
||||||
|
.get_link_addr(&"__procedure_body__".into())
|
||||||
|
.unwrap();
|
||||||
|
let subroutines = self
|
||||||
|
.linker
|
||||||
|
.link_relative(&"__subroutines__".into())
|
||||||
|
.expect("link error");
|
||||||
|
|
||||||
let mut entry = tisc::Assembler::new();
|
let mut entry = tisc::Assembler::new();
|
||||||
if data_frame_size > 0 {
|
if data_frame_size > 0 {
|
||||||
entry = entry
|
entry = entry.lit(data_frame_size).call("data-frame-alloc");
|
||||||
.lit(data_frame_size)
|
|
||||||
.call("data-frame-alloc");
|
|
||||||
}
|
}
|
||||||
entry = entry
|
entry = entry.call_symbol(LinkAddr::Relative {
|
||||||
.call_symbol( LinkAddr::Relative{ symbol: "__subroutines__".into(), offset: body_addr });
|
symbol: "__subroutines__".into(),
|
||||||
|
offset: body_addr,
|
||||||
|
});
|
||||||
|
|
||||||
if data_frame_size > 0 {
|
if data_frame_size > 0 {
|
||||||
entry = entry
|
entry = entry.lit(data_frame_size).call("data-frame-drop");
|
||||||
.lit(data_frame_size)
|
|
||||||
.call("data-frame-drop");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut superlink = tisc::Linker::new();
|
let mut superlink = tisc::Linker::new();
|
||||||
|
@ -79,17 +79,21 @@ impl ProcedureCompiler {
|
||||||
|
|
||||||
match self.symbols.read().unwrap().get(var_id) {
|
match self.symbols.read().unwrap().get(var_id) {
|
||||||
Some(SymbolDef::FrameRef { typ, stack_ref }) => {
|
Some(SymbolDef::FrameRef { typ, stack_ref }) => {
|
||||||
self.asm = self.asm
|
self.asm = self.asm.lit(stack_ref).call("data-frame-set");
|
||||||
.lit(stack_ref)
|
|
||||||
.call("data-frame-set");
|
|
||||||
}
|
}
|
||||||
Some(SymbolDef::StaticRef { typ, link_addr }) => {
|
Some(SymbolDef::StaticRef { typ, link_addr }) => {
|
||||||
self.asm = self.asm
|
self.asm = self
|
||||||
|
.asm
|
||||||
.static_ref(var_id.as_str())
|
.static_ref(var_id.as_str())
|
||||||
.inst(tisc::VM_Instruction::Store);
|
.inst(tisc::VM_Instruction::Store);
|
||||||
}
|
}
|
||||||
Some(SymbolDef::Procedure{ in_types, out_types, link_addr }) => {
|
Some(SymbolDef::Procedure {
|
||||||
self.asm = self.asm
|
in_types,
|
||||||
|
out_types,
|
||||||
|
link_addr,
|
||||||
|
}) => {
|
||||||
|
self.asm = self
|
||||||
|
.asm
|
||||||
.call(var_id.as_str())
|
.call(var_id.as_str())
|
||||||
.inst(tisc::VM_Instruction::Store);
|
.inst(tisc::VM_Instruction::Store);
|
||||||
}
|
}
|
||||||
|
@ -98,39 +102,33 @@ impl ProcedureCompiler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Statement::LetAssign{ typ, var_id, val_expr } => {
|
Statement::LetAssign {
|
||||||
match val_expr {
|
typ,
|
||||||
|
var_id,
|
||||||
|
val_expr,
|
||||||
|
} => match val_expr {
|
||||||
LTExpr::Abstraction { args: _, body: _ } => {
|
LTExpr::Abstraction { args: _, body: _ } => {
|
||||||
self.symbols.write().unwrap()
|
self.symbols
|
||||||
.declare_proc(
|
.write()
|
||||||
var_id.clone(),
|
.unwrap()
|
||||||
vec![],vec![]
|
.declare_proc(var_id.clone(), vec![], vec![]);
|
||||||
);
|
let lambda_procedure = ProcedureCompiler::new(&self.symbols)
|
||||||
let lambda_procedure =
|
|
||||||
ProcedureCompiler::new(&self.symbols)
|
|
||||||
.compile(val_expr)
|
.compile(val_expr)
|
||||||
.into_asm(var_id);
|
.into_asm(var_id);
|
||||||
|
|
||||||
self.linker.add_procedure(
|
self.linker.add_procedure(var_id, lambda_procedure);
|
||||||
var_id,
|
|
||||||
lambda_procedure
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
self.symbols.write().unwrap()
|
self.symbols
|
||||||
.declare_var(
|
.write()
|
||||||
var_id.clone(),
|
.unwrap()
|
||||||
laddertypes::TypeTerm::unit()
|
.declare_var(var_id.clone(), laddertypes::TypeTerm::unit());
|
||||||
);
|
self = self.compile_statement(&Statement::Assignment {
|
||||||
self = self.compile_statement(
|
|
||||||
&Statement::Assignment {
|
|
||||||
var_id: var_id.clone(),
|
var_id: var_id.clone(),
|
||||||
val_expr: val_expr.clone()
|
val_expr: val_expr.clone(),
|
||||||
}
|
});
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
Statement::WhileLoop { condition, body } => {
|
Statement::WhileLoop { condition, body } => {
|
||||||
let asm = self.asm;
|
let asm = self.asm;
|
||||||
|
|
||||||
|
@ -159,24 +157,24 @@ impl ProcedureCompiler {
|
||||||
|
|
||||||
pub fn compile(mut self, expr: <Expr) -> Self {
|
pub fn compile(mut self, expr: <Expr) -> Self {
|
||||||
match expr {
|
match expr {
|
||||||
LTExpr::Symbol { typ, symbol } => {
|
LTExpr::Symbol { typ, symbol } => match self.symbols.read().unwrap().get(symbol) {
|
||||||
match self.symbols.read().unwrap().get(symbol) {
|
|
||||||
Some(SymbolDef::FrameRef { typ, stack_ref }) => {
|
Some(SymbolDef::FrameRef { typ, stack_ref }) => {
|
||||||
self.asm = self.asm
|
self.asm = self.asm.lit(stack_ref).call("data-frame-get");
|
||||||
.lit( stack_ref )
|
|
||||||
.call("data-frame-get");
|
|
||||||
}
|
}
|
||||||
Some(SymbolDef::StaticRef { typ, link_addr }) => {
|
Some(SymbolDef::StaticRef { typ, link_addr }) => {
|
||||||
self.asm = self.asm.static_ref(symbol.as_str());
|
self.asm = self.asm.static_ref(symbol.as_str());
|
||||||
}
|
}
|
||||||
Some(SymbolDef::Procedure{ in_types, out_types, link_addr }) => {
|
Some(SymbolDef::Procedure {
|
||||||
|
in_types,
|
||||||
|
out_types,
|
||||||
|
link_addr,
|
||||||
|
}) => {
|
||||||
self.asm = self.asm.call(symbol.as_str());
|
self.asm = self.asm.call(symbol.as_str());
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
eprintln!("undefined symbol '{}'!", symbol);
|
eprintln!("undefined symbol '{}'!", symbol);
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
|
||||||
LTExpr::Literal { typ, val } => {
|
LTExpr::Literal { typ, val } => {
|
||||||
self.asm = self.asm.lit(*val);
|
self.asm = self.asm.lit(*val);
|
||||||
}
|
}
|
||||||
|
@ -189,22 +187,23 @@ impl ProcedureCompiler {
|
||||||
LTExpr::Abstraction { args, body } => {
|
LTExpr::Abstraction { args, body } => {
|
||||||
for (arg_name, arg_type) in args.iter() {
|
for (arg_name, arg_type) in args.iter() {
|
||||||
if let Some(Ok(typeterm)) = arg_type {
|
if let Some(Ok(typeterm)) = arg_type {
|
||||||
let id = self.symbols
|
let id = self
|
||||||
.write().unwrap()
|
.symbols
|
||||||
.declare_var(
|
.write()
|
||||||
arg_name.clone(),
|
.unwrap()
|
||||||
typeterm.clone()
|
.declare_var(arg_name.clone(), typeterm.clone());
|
||||||
);
|
self.asm = self.asm.lit(id).call("data-frame-set");
|
||||||
self.asm = self.asm
|
|
||||||
.lit( id )
|
|
||||||
.call("data-frame-set");
|
|
||||||
} else {
|
} else {
|
||||||
eprintln!("invalid type {:?} for argument {}", arg_type, arg_name);
|
eprintln!("invalid type {:?} for argument {}", arg_type, arg_name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self = self.compile(body);
|
self = self.compile(body);
|
||||||
}
|
}
|
||||||
LTExpr::Branch { condition, if_expr, else_expr } => {
|
LTExpr::Branch {
|
||||||
|
condition,
|
||||||
|
if_expr,
|
||||||
|
else_expr,
|
||||||
|
} => {
|
||||||
self = self.compile(condition);
|
self = self.compile(condition);
|
||||||
|
|
||||||
let asm = self.asm;
|
let asm = self.asm;
|
||||||
|
@ -226,4 +225,3 @@ impl ProcedureCompiler {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
150
src/runtime.rs
150
src/runtime.rs
|
@ -1,11 +1,7 @@
|
||||||
use {
|
use {
|
||||||
|
crate::{expr::LTExpr, procedure_compiler::ProcedureCompiler, symbols::Scope},
|
||||||
std::sync::{Arc, RwLock},
|
std::sync::{Arc, RwLock},
|
||||||
crate::{
|
tisc::linker::Linker,
|
||||||
expr::LTExpr,
|
|
||||||
symbols::Scope,
|
|
||||||
procedure_compiler::ProcedureCompiler
|
|
||||||
},
|
|
||||||
tisc::linker::Linker
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn init_runtime(linker: &mut Linker) -> Arc<RwLock<Scope>> {
|
pub fn init_runtime(linker: &mut Linker) -> Arc<RwLock<Scope>> {
|
||||||
|
@ -19,12 +15,15 @@ pub fn init_runtime(linker: &mut Linker) -> Arc<RwLock<Scope>> {
|
||||||
"dup",
|
"dup",
|
||||||
vec!["T"],
|
vec!["T"],
|
||||||
vec!["T~machine::Word"],
|
vec!["T~machine::Word"],
|
||||||
vec![ "T~machine::Word",
|
vec!["T~machine::Word", "T~machine::Word"],
|
||||||
"T~machine::Word" ]
|
|
||||||
);
|
);
|
||||||
|
|
||||||
linker.add_procedure("dup", tisc::Assembler::new().inst( tisc::VM_Instruction::Dup ).build());
|
linker.add_procedure(
|
||||||
|
"dup",
|
||||||
|
tisc::Assembler::new()
|
||||||
|
.inst(tisc::VM_Instruction::Dup)
|
||||||
|
.build(),
|
||||||
|
);
|
||||||
|
|
||||||
/* Put a single Ascii character on stdout
|
/* Put a single Ascii character on stdout
|
||||||
*/
|
*/
|
||||||
|
@ -32,11 +31,15 @@ pub fn init_runtime(linker: &mut Linker) -> Arc<RwLock<Scope>> {
|
||||||
"emit",
|
"emit",
|
||||||
vec![],
|
vec![],
|
||||||
vec!["Char~Ascii~machine::Word"],
|
vec!["Char~Ascii~machine::Word"],
|
||||||
vec![]
|
vec![],
|
||||||
);
|
);
|
||||||
|
|
||||||
linker.add_procedure("emit", tisc::Assembler::new().inst( tisc::VM_Instruction::Emit ).build());
|
linker.add_procedure(
|
||||||
|
"emit",
|
||||||
|
tisc::Assembler::new()
|
||||||
|
.inst(tisc::VM_Instruction::Emit)
|
||||||
|
.build(),
|
||||||
|
);
|
||||||
|
|
||||||
/* The top two items must be native u64 integers,
|
/* The top two items must be native u64 integers,
|
||||||
* which are replaced by their sum.
|
* which are replaced by their sum.
|
||||||
|
@ -46,45 +49,49 @@ pub fn init_runtime(linker: &mut Linker) -> Arc<RwLock<Scope>> {
|
||||||
symbols.write().unwrap().declare_proc_parse(
|
symbols.write().unwrap().declare_proc_parse(
|
||||||
"i+",
|
"i+",
|
||||||
vec![],
|
vec![],
|
||||||
vec![ "ℤ_2^64~machine::UInt64~machine::Word",
|
vec![
|
||||||
"ℤ_2^64~machine::UInt64~machine::Word" ],
|
"ℤ_2^64~machine::UInt64~machine::Word",
|
||||||
vec![ "ℤ_2^64~machine::UInt64~machine::Word" ]
|
"ℤ_2^64~machine::UInt64~machine::Word",
|
||||||
|
],
|
||||||
|
vec!["ℤ_2^64~machine::UInt64~machine::Word"],
|
||||||
);
|
);
|
||||||
|
|
||||||
linker.add_procedure(
|
linker.add_procedure(
|
||||||
"i+",
|
"i+",
|
||||||
tisc::Assembler::new()
|
tisc::Assembler::new()
|
||||||
.inst(tisc::VM_Instruction::Add)
|
.inst(tisc::VM_Instruction::Add)
|
||||||
.build()
|
.build(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
/* Floating-point Additionu
|
/* Floating-point Additionu
|
||||||
*/
|
*/
|
||||||
symbols.write().unwrap().declare_proc_parse(
|
symbols.write().unwrap().declare_proc_parse(
|
||||||
"f+",
|
"f+",
|
||||||
vec![],
|
vec![],
|
||||||
vec![ "ℝ~machine::f64~machine::Word",
|
vec![
|
||||||
"ℝ~machine::f64~machine::Word" ],
|
"ℝ~machine::f64~machine::Word",
|
||||||
vec![ "ℝ~machine::f64~machine::Word" ]
|
"ℝ~machine::f64~machine::Word",
|
||||||
|
],
|
||||||
|
vec!["ℝ~machine::f64~machine::Word"],
|
||||||
);
|
);
|
||||||
|
|
||||||
linker.add_procedure(
|
linker.add_procedure(
|
||||||
"f+",
|
"f+",
|
||||||
tisc::Assembler::new()
|
tisc::Assembler::new()
|
||||||
.inst(tisc::VM_Instruction::Addf)
|
.inst(tisc::VM_Instruction::Addf)
|
||||||
.build()
|
.build(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
/* Integer Subtraction
|
/* Integer Subtraction
|
||||||
*/
|
*/
|
||||||
symbols.write().unwrap().declare_proc_parse(
|
symbols.write().unwrap().declare_proc_parse(
|
||||||
"i-",
|
"i-",
|
||||||
vec![],
|
vec![],
|
||||||
vec![ "ℤ_2^64~machine::UInt64~machine::Word",
|
vec![
|
||||||
"ℤ_2^64~machine::UInt64~machine::Word" ],
|
"ℤ_2^64~machine::UInt64~machine::Word",
|
||||||
vec![ "ℤ_2^64~machine::UInt64~machine::Word" ]
|
"ℤ_2^64~machine::UInt64~machine::Word",
|
||||||
|
],
|
||||||
|
vec!["ℤ_2^64~machine::UInt64~machine::Word"],
|
||||||
);
|
);
|
||||||
linker.add_procedure(
|
linker.add_procedure(
|
||||||
"i-",
|
"i-",
|
||||||
|
@ -94,40 +101,40 @@ pub fn init_runtime(linker: &mut Linker) -> Arc<RwLock<Scope>> {
|
||||||
.lit(1)
|
.lit(1)
|
||||||
.inst(tisc::VM_Instruction::Add)
|
.inst(tisc::VM_Instruction::Add)
|
||||||
.inst(tisc::VM_Instruction::Add)
|
.inst(tisc::VM_Instruction::Add)
|
||||||
.build()
|
.build(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
/* Fetch memory address
|
/* Fetch memory address
|
||||||
*/
|
*/
|
||||||
symbols.write().unwrap().declare_proc_parse("@",
|
symbols.write().unwrap().declare_proc_parse(
|
||||||
|
"@",
|
||||||
vec![],
|
vec![],
|
||||||
vec!["<MutRef T~machine::Word>~machine::Address~machine::Word"],
|
vec!["<MutRef T~machine::Word>~machine::Address~machine::Word"],
|
||||||
vec![ "T~machine::Word" ]
|
vec!["T~machine::Word"],
|
||||||
);
|
);
|
||||||
linker.add_procedure(
|
linker.add_procedure(
|
||||||
"@",
|
"@",
|
||||||
tisc::Assembler::new()
|
tisc::Assembler::new()
|
||||||
.inst(tisc::VM_Instruction::Fetch)
|
.inst(tisc::VM_Instruction::Fetch)
|
||||||
.build()
|
.build(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
/* Store to memory
|
/* Store to memory
|
||||||
*/
|
*/
|
||||||
symbols.write().unwrap().declare_proc_parse("!",
|
symbols.write().unwrap().declare_proc_parse(
|
||||||
|
"!",
|
||||||
vec![],
|
vec![],
|
||||||
vec![
|
vec![
|
||||||
"<MutRef T~machine::Word>~machine::Address~machine::Word",
|
"<MutRef T~machine::Word>~machine::Address~machine::Word",
|
||||||
"T~machine::Word"
|
"T~machine::Word",
|
||||||
],
|
],
|
||||||
vec![]
|
vec![],
|
||||||
);
|
);
|
||||||
linker.add_procedure(
|
linker.add_procedure(
|
||||||
"!",
|
"!",
|
||||||
tisc::Assembler::new()
|
tisc::Assembler::new()
|
||||||
.inst(tisc::VM_Instruction::Store)
|
.inst(tisc::VM_Instruction::Store)
|
||||||
.build()
|
.build(),
|
||||||
);
|
);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -143,70 +150,68 @@ pub fn init_runtime(linker: &mut Linker) -> Arc<RwLock<Scope>> {
|
||||||
symbols.write().unwrap().declare_proc_parse(
|
symbols.write().unwrap().declare_proc_parse(
|
||||||
"i*",
|
"i*",
|
||||||
vec![],
|
vec![],
|
||||||
vec![ "ℤ_2^64~machine::UInt64~machine::Word",
|
vec![
|
||||||
"ℤ_2^64~machine::UInt64~machine::Word" ],
|
"ℤ_2^64~machine::UInt64~machine::Word",
|
||||||
vec![ "ℤ_2^64~machine::UInt64~machine::Word" ]);
|
"ℤ_2^64~machine::UInt64~machine::Word",
|
||||||
|
],
|
||||||
|
vec!["ℤ_2^64~machine::UInt64~machine::Word"],
|
||||||
|
);
|
||||||
|
|
||||||
linker.add_procedure(
|
linker.add_procedure(
|
||||||
"i*",
|
"i*",
|
||||||
tisc::Assembler::new()
|
tisc::Assembler::new()
|
||||||
.lit(0) // [ a b ] -- [ a b sum ]
|
.lit(0) // [ a b ] -- [ a b sum ]
|
||||||
|
|
||||||
.while_loop(
|
.while_loop(
|
||||||
// condition
|
// condition
|
||||||
tisc::Assembler::new()
|
tisc::Assembler::new()
|
||||||
// [ a b sum ] -- [ a b sum b ]
|
// [ a b sum ] -- [ a b sum b ]
|
||||||
.lit(2)
|
.lit(2)
|
||||||
.inst(tisc::VM_Instruction::Pick),
|
.inst(tisc::VM_Instruction::Pick),
|
||||||
|
|
||||||
// body
|
// body
|
||||||
tisc::Assembler::new()
|
tisc::Assembler::new()
|
||||||
// [ a b sum ] -- [ a b sum a ]
|
// [ a b sum ] -- [ a b sum a ]
|
||||||
.lit(3)
|
.lit(3)
|
||||||
.inst(tisc::VM_Instruction::Pick)
|
.inst(tisc::VM_Instruction::Pick)
|
||||||
|
|
||||||
// [ a b sum a -- a b (sum+a) ]
|
// [ a b sum a -- a b (sum+a) ]
|
||||||
.inst(tisc::VM_Instruction::Add)
|
.inst(tisc::VM_Instruction::Add)
|
||||||
|
|
||||||
// [ a b sum -- a sum b ]
|
// [ a b sum -- a sum b ]
|
||||||
.inst(tisc::VM_Instruction::Swap)
|
.inst(tisc::VM_Instruction::Swap)
|
||||||
|
|
||||||
// [ a sum b -- a sum b 1 ]
|
// [ a sum b -- a sum b 1 ]
|
||||||
.lit(1)
|
.lit(1)
|
||||||
|
|
||||||
// [ a sum b -- a sum (b-1) ]
|
// [ a sum b -- a sum (b-1) ]
|
||||||
.inst(tisc::VM_Instruction::Swap)
|
.inst(tisc::VM_Instruction::Swap)
|
||||||
.call("i-")
|
.call("i-")
|
||||||
|
|
||||||
// [ a sum b -- a b sum ]
|
// [ a sum b -- a b sum ]
|
||||||
.inst( tisc::VM_Instruction::Swap )
|
.inst(tisc::VM_Instruction::Swap),
|
||||||
)
|
)
|
||||||
|
|
||||||
// [ a b sum -- a sum b ]
|
// [ a b sum -- a sum b ]
|
||||||
.lit(2).inst(tisc::VM_Instruction::Roll)
|
.lit(2)
|
||||||
|
.inst(tisc::VM_Instruction::Roll)
|
||||||
// [ a sum b -- a sum ]
|
// [ a sum b -- a sum ]
|
||||||
.inst(tisc::VM_Instruction::Drop)
|
.inst(tisc::VM_Instruction::Drop)
|
||||||
|
|
||||||
// [ a sum -- sum a ]
|
// [ a sum -- sum a ]
|
||||||
.lit(2).inst(tisc::VM_Instruction::Roll)
|
.lit(2)
|
||||||
|
.inst(tisc::VM_Instruction::Roll)
|
||||||
// [ sum a -- sum ]
|
// [ sum a -- sum ]
|
||||||
.inst(tisc::VM_Instruction::Drop)
|
.inst(tisc::VM_Instruction::Drop)
|
||||||
|
.build(),
|
||||||
.build()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
symbols.write().unwrap().declare_static_parse(
|
symbols.write().unwrap().declare_static_parse(
|
||||||
"data-frame-ptr",
|
"data-frame-ptr",
|
||||||
"<MutRef <Seq machine::Word>>~machine::Address~machine::Word"
|
"<MutRef <Seq machine::Word>>~machine::Address~machine::Word",
|
||||||
);
|
);
|
||||||
linker.add_static("data-frame-ptr", vec![0x1000]);
|
linker.add_static("data-frame-ptr", vec![0x1000]);
|
||||||
|
|
||||||
symbols.write().unwrap().declare_proc_parse(
|
symbols.write().unwrap().declare_proc_parse(
|
||||||
"data-frame-set",
|
"data-frame-set",
|
||||||
vec!["T"],
|
vec!["T"],
|
||||||
vec!["T~machine::Word",
|
vec![
|
||||||
"<RefMut T~machine::Word>~LocalVariableId~machine::UInt64~machine::Word"],
|
"T~machine::Word",
|
||||||
vec![]);
|
"<RefMut T~machine::Word>~LocalVariableId~machine::UInt64~machine::Word",
|
||||||
|
],
|
||||||
|
vec![],
|
||||||
|
);
|
||||||
|
|
||||||
linker.add_procedure(
|
linker.add_procedure(
|
||||||
"data-frame-set",
|
"data-frame-set",
|
||||||
|
@ -215,15 +220,14 @@ pub fn init_runtime(linker: &mut Linker) -> Arc<RwLock<Scope>> {
|
||||||
.inst(tisc::VM_Instruction::Fetch)
|
.inst(tisc::VM_Instruction::Fetch)
|
||||||
.inst(tisc::VM_Instruction::Add)
|
.inst(tisc::VM_Instruction::Add)
|
||||||
.inst(tisc::VM_Instruction::Store)
|
.inst(tisc::VM_Instruction::Store)
|
||||||
.build()
|
.build(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
symbols.write().unwrap().declare_proc_parse(
|
symbols.write().unwrap().declare_proc_parse(
|
||||||
"data-frame-get",
|
"data-frame-get",
|
||||||
vec!["T"],
|
vec!["T"],
|
||||||
vec!["<Ref T~machine::Word>~DataFrameRef~machine::UInt64~machine::Word"],
|
vec!["<Ref T~machine::Word>~DataFrameRef~machine::UInt64~machine::Word"],
|
||||||
vec!["T~machine::Word"]
|
vec!["T~machine::Word"],
|
||||||
);
|
);
|
||||||
linker.add_procedure(
|
linker.add_procedure(
|
||||||
"data-frame-get",
|
"data-frame-get",
|
||||||
|
@ -232,15 +236,13 @@ pub fn init_runtime(linker: &mut Linker) -> Arc<RwLock<Scope>> {
|
||||||
.inst(tisc::VM_Instruction::Fetch)
|
.inst(tisc::VM_Instruction::Fetch)
|
||||||
.inst(tisc::VM_Instruction::Add)
|
.inst(tisc::VM_Instruction::Add)
|
||||||
.inst(tisc::VM_Instruction::Fetch)
|
.inst(tisc::VM_Instruction::Fetch)
|
||||||
.build()
|
.build(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
symbols
|
||||||
symbols.write().unwrap().declare_proc_parse(
|
.write()
|
||||||
"data-frame-alloc",
|
.unwrap()
|
||||||
vec![],
|
.declare_proc_parse("data-frame-alloc", vec![], vec![], vec![]);
|
||||||
vec![],
|
|
||||||
vec![]);
|
|
||||||
|
|
||||||
linker.add_procedure(
|
linker.add_procedure(
|
||||||
"data-frame-alloc",
|
"data-frame-alloc",
|
||||||
|
@ -250,14 +252,13 @@ pub fn init_runtime(linker: &mut Linker) -> Arc<RwLock<Scope>> {
|
||||||
.call("i-")
|
.call("i-")
|
||||||
.static_ref("data-frame-ptr")
|
.static_ref("data-frame-ptr")
|
||||||
.inst(tisc::VM_Instruction::Store)
|
.inst(tisc::VM_Instruction::Store)
|
||||||
.build()
|
.build(),
|
||||||
);
|
);
|
||||||
|
|
||||||
symbols.write().unwrap().declare_proc_parse(
|
symbols
|
||||||
"data-frame-drop",
|
.write()
|
||||||
vec![],
|
.unwrap()
|
||||||
vec![],
|
.declare_proc_parse("data-frame-drop", vec![], vec![], vec![]);
|
||||||
vec![]);
|
|
||||||
|
|
||||||
linker.add_procedure(
|
linker.add_procedure(
|
||||||
"data-frame-drop",
|
"data-frame-drop",
|
||||||
|
@ -267,9 +268,8 @@ pub fn init_runtime(linker: &mut Linker) -> Arc<RwLock<Scope>> {
|
||||||
.call("i+")
|
.call("i+")
|
||||||
.static_ref("data-frame-ptr")
|
.static_ref("data-frame-ptr")
|
||||||
.inst(tisc::VM_Instruction::Store)
|
.inst(tisc::VM_Instruction::Store)
|
||||||
.build()
|
.build(),
|
||||||
);
|
);
|
||||||
|
|
||||||
symbols
|
symbols
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
139
src/symbols.rs
139
src/symbols.rs
|
@ -1,9 +1,9 @@
|
||||||
use {
|
use {
|
||||||
|
crate::expr::LTExpr,
|
||||||
std::{
|
std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
sync::{Arc, RwLock},
|
sync::{Arc, RwLock},
|
||||||
},
|
},
|
||||||
crate::expr::LTExpr
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
|
@ -14,27 +14,36 @@ pub enum SymbolDef {
|
||||||
},
|
},
|
||||||
StaticRef {
|
StaticRef {
|
||||||
typ: laddertypes::TypeTerm,
|
typ: laddertypes::TypeTerm,
|
||||||
link_addr: Option< tisc::VM_Word >
|
link_addr: Option<tisc::VM_Word>,
|
||||||
},
|
},
|
||||||
Procedure {
|
Procedure {
|
||||||
in_types: Vec<laddertypes::TypeTerm>,
|
in_types: Vec<laddertypes::TypeTerm>,
|
||||||
out_types: Vec<laddertypes::TypeTerm>,
|
out_types: Vec<laddertypes::TypeTerm>,
|
||||||
link_addr: Option< tisc::VM_Word >
|
link_addr: Option<tisc::VM_Word>,
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SymbolDef {
|
impl SymbolDef {
|
||||||
pub fn get_type(&self, typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>) -> laddertypes::TypeTerm {
|
pub fn get_type(
|
||||||
|
&self,
|
||||||
|
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
|
||||||
|
) -> laddertypes::TypeTerm {
|
||||||
match self {
|
match self {
|
||||||
SymbolDef::FrameRef { typ, stack_ref: _ } => typ.clone(),
|
SymbolDef::FrameRef { typ, stack_ref: _ } => typ.clone(),
|
||||||
SymbolDef::StaticRef { typ, link_addr: _ } => typ.clone(),
|
SymbolDef::StaticRef { typ, link_addr: _ } => typ.clone(),
|
||||||
SymbolDef::Procedure { in_types, out_types, link_addr: _ } => {
|
SymbolDef::Procedure {
|
||||||
laddertypes::TypeTerm::App(vec![
|
in_types,
|
||||||
typectx.write().unwrap().parse("Fn").expect("parse typeterm"),
|
out_types,
|
||||||
|
link_addr: _,
|
||||||
|
} => laddertypes::TypeTerm::App(vec![
|
||||||
|
typectx
|
||||||
|
.write()
|
||||||
|
.unwrap()
|
||||||
|
.parse("Fn")
|
||||||
|
.expect("parse typeterm"),
|
||||||
laddertypes::TypeTerm::App(in_types.clone()),
|
laddertypes::TypeTerm::App(in_types.clone()),
|
||||||
laddertypes::TypeTerm::App( out_types.clone() )
|
laddertypes::TypeTerm::App(out_types.clone()),
|
||||||
])
|
]),
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,7 +67,7 @@ pub struct Scope {
|
||||||
/* parent scope whose all
|
/* parent scope whose all
|
||||||
* symbols are inherited
|
* symbols are inherited
|
||||||
*/
|
*/
|
||||||
parent: Option< Arc<RwLock<Scope>> >
|
parent: Option<Arc<RwLock<Scope>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Scope {
|
impl Scope {
|
||||||
|
@ -67,7 +76,7 @@ impl Scope {
|
||||||
symbols: HashMap::new(),
|
symbols: HashMap::new(),
|
||||||
typectx: Arc::new(RwLock::new(laddertypes::dict::TypeDict::new())),
|
typectx: Arc::new(RwLock::new(laddertypes::dict::TypeDict::new())),
|
||||||
frame_size: 0,
|
frame_size: 0,
|
||||||
parent: None
|
parent: None,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,12 +104,11 @@ impl Scope {
|
||||||
None => {
|
None => {
|
||||||
if let Some(parent) = self.parent.as_ref() {
|
if let Some(parent) = self.parent.as_ref() {
|
||||||
match parent.read().unwrap().get(name) {
|
match parent.read().unwrap().get(name) {
|
||||||
Some(SymbolDef::FrameRef {
|
Some(SymbolDef::FrameRef { typ, stack_ref }) => Some(SymbolDef::FrameRef {
|
||||||
typ, stack_ref
|
typ: typ.clone(),
|
||||||
}) => Some(SymbolDef::FrameRef {
|
stack_ref: stack_ref + self.get_frame_size() as i64,
|
||||||
typ: typ.clone(), stack_ref: stack_ref + self.get_frame_size() as i64
|
|
||||||
}),
|
}),
|
||||||
x => x.clone()
|
x => x.clone(),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -124,11 +132,12 @@ impl Scope {
|
||||||
*/
|
*/
|
||||||
//<><><><><><>
|
//<><><><><><>
|
||||||
|
|
||||||
pub fn declare_proc_parse(&mut self,
|
pub fn declare_proc_parse(
|
||||||
|
&mut self,
|
||||||
name: &str,
|
name: &str,
|
||||||
type_vars: Vec<&str>,
|
type_vars: Vec<&str>,
|
||||||
in_types: Vec<&str>,
|
in_types: Vec<&str>,
|
||||||
out_types: Vec<&str>
|
out_types: Vec<&str>,
|
||||||
) {
|
) {
|
||||||
for v in type_vars {
|
for v in type_vars {
|
||||||
self.typectx.write().unwrap().add_varname(v.into());
|
self.typectx.write().unwrap().add_varname(v.into());
|
||||||
|
@ -136,70 +145,86 @@ impl Scope {
|
||||||
|
|
||||||
self.declare_proc(
|
self.declare_proc(
|
||||||
String::from(name),
|
String::from(name),
|
||||||
in_types.into_iter().map(|t| self.typectx.write().unwrap().parse(t).expect("parse typeterm")).collect(),
|
in_types
|
||||||
out_types.into_iter().map(|t| self.typectx.write().unwrap().parse(t).expect("parse typeterm")).collect()
|
.into_iter()
|
||||||
|
.map(|t| {
|
||||||
|
self.typectx
|
||||||
|
.write()
|
||||||
|
.unwrap()
|
||||||
|
.parse(t)
|
||||||
|
.expect("parse typeterm")
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
out_types
|
||||||
|
.into_iter()
|
||||||
|
.map(|t| {
|
||||||
|
self.typectx
|
||||||
|
.write()
|
||||||
|
.unwrap()
|
||||||
|
.parse(t)
|
||||||
|
.expect("parse typeterm")
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn declare_proc(&mut self,
|
pub fn declare_proc(
|
||||||
|
&mut self,
|
||||||
name: String,
|
name: String,
|
||||||
in_types: Vec<laddertypes::TypeTerm>,
|
in_types: Vec<laddertypes::TypeTerm>,
|
||||||
out_types: Vec< laddertypes::TypeTerm >
|
out_types: Vec<laddertypes::TypeTerm>,
|
||||||
) {
|
) {
|
||||||
self.symbols.insert(name, SymbolDef::Procedure {
|
self.symbols.insert(
|
||||||
|
name,
|
||||||
|
SymbolDef::Procedure {
|
||||||
link_addr: None, //LinkAddr::Relative{ name, offset: 0 },
|
link_addr: None, //LinkAddr::Relative{ name, offset: 0 },
|
||||||
in_types,
|
in_types,
|
||||||
out_types
|
out_types,
|
||||||
});
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
//<><><><><>
|
//<><><><><>
|
||||||
|
|
||||||
pub fn declare_var_parse(&mut self,
|
pub fn declare_var_parse(&mut self, name: &str, typ: &str) {
|
||||||
name: &str,
|
let typ = self
|
||||||
typ: &str
|
.typectx
|
||||||
) {
|
.write()
|
||||||
let typ = self.typectx.write().unwrap().parse(typ).expect("parse typeterm");
|
.unwrap()
|
||||||
self.declare_var(
|
.parse(typ)
|
||||||
String::from(name),
|
.expect("parse typeterm");
|
||||||
typ
|
self.declare_var(String::from(name), typ);
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn declare_var(&mut self, name: String, typ: laddertypes::TypeTerm) -> tisc::VM_Word {
|
pub fn declare_var(&mut self, name: String, typ: laddertypes::TypeTerm) -> tisc::VM_Word {
|
||||||
let stack_ref = self.frame_size as tisc::VM_Word;
|
let stack_ref = self.frame_size as tisc::VM_Word;
|
||||||
self.frame_size += 1;
|
self.frame_size += 1;
|
||||||
|
|
||||||
self.symbols.insert(name, SymbolDef::FrameRef {
|
self.symbols
|
||||||
typ,
|
.insert(name, SymbolDef::FrameRef { typ, stack_ref });
|
||||||
stack_ref
|
|
||||||
});
|
|
||||||
|
|
||||||
stack_ref
|
stack_ref
|
||||||
}
|
}
|
||||||
|
|
||||||
//<><><><><><>
|
//<><><><><><>
|
||||||
|
|
||||||
pub fn declare_static_parse(
|
pub fn declare_static_parse(&mut self, name: &str, typ: &str) {
|
||||||
&mut self,
|
let typ = self
|
||||||
name: &str,
|
.typectx
|
||||||
typ: &str
|
.write()
|
||||||
) {
|
.unwrap()
|
||||||
let typ = self.typectx
|
.parse(typ)
|
||||||
.write().unwrap()
|
.expect("parse typeterm");
|
||||||
.parse(typ).expect("parse typeterm");
|
|
||||||
self.declare_static(String::from(name), typ);
|
self.declare_static(String::from(name), typ);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn declare_static(
|
pub fn declare_static(&mut self, name: String, typ: laddertypes::TypeTerm) {
|
||||||
&mut self,
|
self.symbols.insert(
|
||||||
name: String,
|
name,
|
||||||
typ: laddertypes::TypeTerm
|
SymbolDef::StaticRef {
|
||||||
) {
|
|
||||||
self.symbols.insert(name, SymbolDef::StaticRef {
|
|
||||||
typ,
|
typ,
|
||||||
link_addr: None
|
link_addr: None,
|
||||||
});
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue