Compare commits

..

3 commits

4 changed files with 513 additions and 279 deletions

View file

@ -52,6 +52,7 @@ where
It: std::iter::Iterator<Item = char>, It: std::iter::Iterator<Item = char>,
{ {
chars: std::iter::Peekable<It>, chars: std::iter::Peekable<It>,
position: usize
} }
impl<It> LTIRLexer<It> impl<It> LTIRLexer<It>
@ -70,6 +71,23 @@ where
fn from(chars: It) -> Self { fn from(chars: It) -> Self {
LTIRLexer { LTIRLexer {
chars: chars.peekable(), chars: chars.peekable(),
position: 0,
}
}
}
#[derive(Clone, Debug)]
pub struct InputRegionTag {
begin: usize,
end: usize
}
impl InputRegionTag {
pub fn max( a: InputRegionTag, b: InputRegionTag ) -> InputRegionTag {
InputRegionTag {
begin: usize::min( a.begin, b.begin ),
end: usize::max( a.end, b.end )
} }
} }
} }
@ -78,10 +96,14 @@ impl<It> Iterator for LTIRLexer<It>
where where
It: Iterator<Item = char>, It: Iterator<Item = char>,
{ {
type Item = Result<LTIRToken, LexError>; type Item = (InputRegionTag, Result<LTIRToken, LexError>);
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let mut state = LexerState::Any; let mut state = LexerState::Any;
let mut region = InputRegionTag{
begin: self.position,
end: self.position
};
while let Some(c) = self.chars.peek() { while let Some(c) = self.chars.peek() {
match &mut state { match &mut state {
@ -89,47 +111,70 @@ where
LexerState::Any => match c { LexerState::Any => match c {
'λ' => { 'λ' => {
self.chars.next(); self.chars.next();
return Some(Ok(LTIRToken::Lambda)); self.position += 1;
region.end += 1;
return Some((region, Ok(LTIRToken::Lambda)));
} }
'.' => { '.' | '↦' => {
self.chars.next(); self.chars.next();
return Some(Ok(LTIRToken::LambdaBody)); self.position += 1;
region.end += 1;
return Some((region, Ok(LTIRToken::LambdaBody)));
} }
'(' => { '(' => {
self.chars.next(); self.chars.next();
return Some(Ok(LTIRToken::ExprOpen)); self.position += 1;
region.end += 1;
return Some((region, Ok(LTIRToken::ExprOpen)));
} }
')' => { ')' => {
self.chars.next(); self.chars.next();
return Some(Ok(LTIRToken::ExprClose)); self.position += 1;
region.end += 1;
return Some((region, Ok(LTIRToken::ExprClose)));
} }
'{' => { '{' => {
self.chars.next(); self.chars.next();
return Some(Ok(LTIRToken::BlockOpen)); self.position += 1;
region.end += 1;
return Some((region, Ok(LTIRToken::BlockOpen)));
} }
'}' => { '}' => {
self.chars.next(); self.chars.next();
return Some(Ok(LTIRToken::BlockClose)); self.position += 1;
region.end += 1;
return Some((region, Ok(LTIRToken::BlockClose)));
} }
':' => { ':' => {
self.chars.next(); self.chars.next();
self.position += 1;
region.end += 1;
state = LexerState::TypeTerm(String::new()); state = LexerState::TypeTerm(String::new());
} }
'=' => { '=' => {
self.chars.next(); self.chars.next();
return Some(Ok(LTIRToken::AssignValue)); self.position += 1;
region.end += 1;
return Some((region, Ok(LTIRToken::AssignValue)));
} }
';' => { ';' => {
self.chars.next(); self.chars.next();
return Some(Ok(LTIRToken::StatementSep)); self.position += 1;
region.end += 1;
return Some((region, Ok(LTIRToken::StatementSep)));
} }
'\'' => { '\'' => {
self.chars.next(); self.chars.next();
self.position += 1;
region.end += 1;
state = LexerState::Char(None); state = LexerState::Char(None);
} }
c => { c => {
if c.is_whitespace() { if c.is_whitespace() {
self.chars.next(); self.chars.next();
self.position += 1;
region.begin += 1;
region.end += 1;
} else if c.is_digit(10) { } else if c.is_digit(10) {
state = LexerState::Num(0); state = LexerState::Num(0);
} else { } else {
@ -139,41 +184,49 @@ where
}, },
LexerState::Char(val) => { LexerState::Char(val) => {
self.position += 2;
region.end += 2;
*val = Some(match self.chars.next() { *val = Some(match self.chars.next() {
Some('\\') => match self.chars.next() { Some('\\') => {
Some('0') => '\0', self.position += 1;
Some('n') => '\n', region.end += 1;
Some('t') => '\t', match self.chars.next() {
Some(c) => c, Some('0') => '\0',
None => { Some('n') => '\n',
return Some(Err(LexError::InvalidChar)); Some('t') => '\t',
Some(c) => c,
None => {
return Some((region, Err(LexError::InvalidChar)));
}
} }
}, },
Some(c) => c, Some(c) => c,
None => { None => {
return Some(Err(LexError::InvalidChar)); return Some((region, Err(LexError::InvalidChar)));
} }
}); });
match self.chars.next() { match self.chars.next() {
Some('\'') => { Some('\'') => {
if let Some(token) = state.clone().into_token() { if let Some(token) = state.clone().into_token() {
return Some(Ok(token)); return Some((region, Ok(token)));
} }
} }
_ => { _ => {
return Some(Err(LexError::InvalidChar)); return Some((region, Err(LexError::InvalidChar)));
} }
} }
} }
LexerState::TypeTerm(s) => { LexerState::TypeTerm(s) => {
if *c == '=' || *c == '.' { if *c == '=' || *c == '.' || *c == '↦' || *c == ';' {
if let Some(token) = state.clone().into_token() { if let Some(token) = state.clone().into_token() {
return Some(Ok(token)); return Some((region, Ok(token)));
} }
} else { } else {
if let Some(c) = self.chars.next() { if let Some(c) = self.chars.next() {
self.position += 1;
region.end += 1;
s.push(c); s.push(c);
} }
} }
@ -189,16 +242,19 @@ where
|| *c == '=' || *c == '='
|| *c == ':' || *c == ':'
|| *c == '.' || *c == '.'
|| *c == '↦'
{ {
// finish the current token // finish the current token
if let Some(token) = state.clone().into_token() { if let Some(token) = state.clone().into_token() {
return Some(Ok(token)); return Some((region, Ok(token)));
} }
} else { } else {
// append to the current token // append to the current token
let c = self.chars.next().unwrap(); let c = self.chars.next().unwrap();
self.position += 1;
region.end += 1;
match &mut state { match &mut state {
LexerState::Sym(s) => { LexerState::Sym(s) => {
@ -209,7 +265,7 @@ where
if let Some(d) = c.to_digit(10) { if let Some(d) = c.to_digit(10) {
*n = (*n) * 10 + d as i64; *n = (*n) * 10 + d as i64;
} else { } else {
return Some(Err(LexError::InvalidDigit)); return Some((region, Err(LexError::InvalidDigit)));
} }
} }
@ -221,7 +277,7 @@ where
} }
if let Some(token) = state.into_token() { if let Some(token) = state.into_token() {
Some(Ok(token)) Some((region, Ok(token)))
} else { } else {
None None
} }
@ -244,8 +300,8 @@ mod tests {
.chars(), .chars(),
); );
for token in lexer { for (range, token) in lexer {
eprintln!("token = {:?}", token); eprintln!("[{:?}] {:?}", range, token);
} }
} }
} }

View file

@ -42,24 +42,6 @@ fn main() {
let main_scope = Scope::with_parent(&root_scope); let main_scope = Scope::with_parent(&root_scope);
let typectx = main_scope.read().unwrap().typectx.clone(); let typectx = main_scope.read().unwrap().typectx.clone();
/* define type of the symbol
*/
main_scope.write().unwrap().declare_static_parse(
"hello-string",
"<Seq Char
~Ascii
~machine::Word>
~<NullTerminatedSeq machine::Word>",
);
main_scope.write().unwrap().declare_static_parse(
"pfxstr",
"<Seq Char
~Ascii
~machine::Word>
~<LengthPrefixedSeq machine::Word>",
);
/* link assembly-program to symbols /* link assembly-program to symbols
*/ */
linker.add_procedure( linker.add_procedure(
@ -68,102 +50,79 @@ fn main() {
&main_scope, &main_scope,
"main", "main",
"{ "{
let print-nullterm = let print-nullterm =
λ str : <Ref <Seq Char~Ascii~machine::Word>> λ{} : < Seq Char~Ascii~machine::Word >
~ <Ref <NullTerminatedArray machine::Word>> ~ < NullTerminatedArray machine::Word >
~ machine::Address {
~ machine::Word while(dup) { emit; }
. emit;
{ };
while (@ str) {
emit (@ str);
! str (i+ str 1);
}
};
let print-len = print-nullterm 'H' 'a' 'l' 'l' 'o' ' ' 'W' 'e' 'l' 't' '!' '\n' '\0';
λ len : _2^64
~ machine::UInt64 let print-uint =
~ machine::Word λx : ~ _2^64 ~ machine::UInt64
. {
λ str : <Ref <Seq Char~Ascii~machine::Word>> if( x ) {
~ <Ref <Array machine::Word>> print-nullterm {
~ machine::Address '\0';
~ machine::Word while( x ) {
. i+ '0' (i% x 10);
{ ! x (i/ x 10);
let end = (i+ str len); }
while (i- str end) { };
emit (@ str); } else {
! str (i+ str 1); emit '0';
} };
};
let int-neg = λx : ~machine::Int64~machine::Word i+ (bit-neg x) 1;
let int-sign = λx : ~machine::Int64~machine::Word bit-and (bit-shr x 63) 1;
let int-lte = λ{
a : ~machine::Int64~machine::Word;
b : ~machine::Int64~machine::Word;
} int-sign (i- a b);
let int-max = λ{
a : ~machine::Int64~machine::Word;
b : ~machine::Int64~machine::Word;
} if( int-lte b a ) { a; } else { b; };
let vec3i-add = λ{
{ ax:_2^64; ay:_2^64; az:_2^64; } : <Vec3 _2^64~machine::UInt64>;
{ bx:_2^64; by:_2^64; bz:_2^64; } : <Vec3 _2^64~machine::UInt64>;
} {
i+ az bz;
i+ ay by;
i+ ax bx;
}; };
let print-lenprefix = let print-vec3i =
λ str : <Ref <Seq Char~Ascii~machine::Word>> λ{ x:_2^64; y:_2^64; z:_2^64; } : <Vec3 _2^64~machine::UInt64>
~ <Ref <LenPrefixArray machine::Word>> {
~ <Ref <Struct print-nullterm '{' 'x' '=' '\0';
<len _2^64 print-uint x;
~machine::UInt64 print-nullterm ';' ' ' 'y' '=' '\0';
~machine::Word> print-uint y;
<data <Array machine::Word>> print-nullterm ';' ' ' 'z' '=' '\0';
>> print-uint z;
~ machine::Address print-nullterm '}' '\0';
~ machine::Word };
.
{
let len = (@ str);
! str (i+ str 1);
print-len len str;
};
let hello = λ.{ let red-u8
print-nullterm hello-string; : <Fn <> Color ~ RGB ~ <Vec3 _0,1 ~ _256 ~ machine::UInt64>>
print-lenprefix pfxstr; = λ{} { 0; 0; 255; };
let green-u8 = λ{} { 0; 255; 0; };
let isquare = λx:. i* x x; let blue-u8 = λ{} { 255; 0; 0; };
let imagnitude2 = λx:.λy:. i+ (isquare x) (isquare y); let yellow-u8 = λ{} { 0; 220; 220; };
let factorial = λn:.
if( n ){ i* n (factorial (i- n 1)); }
else { 1; };
factorial 20;
if ( i- (imagnitude2 10 20) 500 ) {
emit '?';
} else {
emit '!';
};
print-vec3i (vec3i-add green-u8 blue-u8);
emit '\n'; emit '\n';
emit (i+ '0' (isquare 3)); }"
emit '\n';
};
hello;
}",
), ),
); );
linker.add_static(
"hello-string",
"Hallo Welt!\n\0"
.chars()
.map(|c| (c as u8) as tisc::VM_Word)
.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 let main_addr = linker
.get_link_addr(&"main".into()) .get_link_addr(&"main".into())
.expect("'main' not linked"); .expect("'main' not linked");

View file

@ -9,12 +9,13 @@ use {
}, },
}; };
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq)]
pub enum ParseError { pub enum ParseError {
LexError(LexError), LexError(LexError),
UnexpectedClose, UnexpectedClose,
UnexpectedEnd, UnexpectedEnd,
UnexpectedToken, UnexpectedToken,
TypeParseError(laddertypes::parser::ParseError)
} }
pub fn parse_expect<It>( pub fn parse_expect<It>(
@ -25,53 +26,151 @@ where
It: Iterator<Item = char>, It: Iterator<Item = char>,
{ {
match tokens.next() { match tokens.next() {
Some(Ok(t)) => { Some((region, Ok(t))) => {
if t == expected_token { if t == expected_token {
Ok(()) Ok(())
} else { } else {
Err(ParseError::UnexpectedToken) Err(ParseError::UnexpectedToken)
} }
} }
Some(Err(err)) => Err(ParseError::LexError(err)), Some((region, Err(err))) => Err(ParseError::LexError(err)),
None => Err(ParseError::UnexpectedEnd), None => Err(ParseError::UnexpectedEnd),
} }
} }
/* parse symbol name
*/
pub fn parse_symbol<It>(tokens: &mut Peekable<LTIRLexer<It>>) -> Result<String, ParseError> pub fn parse_symbol<It>(tokens: &mut Peekable<LTIRLexer<It>>) -> Result<String, ParseError>
where where
It: Iterator<Item = char>, It: Iterator<Item = char>,
{ {
match tokens.next() { match tokens.next() {
Some(Ok(LTIRToken::Symbol(name))) => Ok(name), Some((region, Ok(LTIRToken::Symbol(name)))) => Ok(name),
Some(Ok(_)) => Err(ParseError::UnexpectedToken), Some((region, Ok(_))) => Err(ParseError::UnexpectedToken),
Some(Err(err)) => Err(ParseError::LexError(err)), Some((region, Err(err))) => Err(ParseError::LexError(err)),
None => Err(ParseError::UnexpectedEnd), None => Err(ParseError::UnexpectedEnd),
} }
} }
/* parse an optional type annotation
* `: T`
*/
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> ) -> Result<Option<laddertypes::TypeTerm>, ParseError>
where where
It: Iterator<Item = char>, It: Iterator<Item = char>,
{ {
if let Some(peektok) = tokens.peek().clone() { if let Some((region, 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) => Some(Ok(typeterm)), Ok(typeterm) => Ok(Some(typeterm)),
Err(parse_error) => Some(Err(TypeError::ParseError(parse_error))), Err(parse_error) => Err(ParseError::TypeParseError(parse_error)),
} }
} }
_ => None, _ => Ok(None),
} }
} else { } else {
None Ok(None)
} }
} }
#[derive(Debug, PartialEq, Eq)]
pub enum VariableBinding {
Atomic {
symbol: String,
typtag: Option<laddertypes::TypeTerm>
},
Struct {
members: Vec< VariableBinding >,
typtag: Option<laddertypes::TypeTerm>
}
}
impl VariableBinding {
pub fn flatten(self) -> Vec<(String, Option<laddertypes::TypeTerm>)> {
match self {
VariableBinding::Atomic{ symbol, typtag } =>
vec![ (symbol, typtag) ],
VariableBinding::Struct{ members, typtag } =>
members
.into_iter()
.map(|a| a.flatten().into_iter())
.flatten()
.collect()
}
}
}
/* parse a symbol binding of the form
* `x`
* or `x : T`
*/
pub fn parse_binding_expr<It>(
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
tokens: &mut Peekable<LTIRLexer<It>>,
) -> Result< VariableBinding, ParseError>
where It: Iterator<Item = char>
{
if let Some((region, peektok)) = tokens.peek().clone() {
match peektok {
Ok(LTIRToken::BlockOpen) => {
Ok(VariableBinding::Struct {
members: parse_binding_block(typectx, tokens)?,
typtag: parse_type_tag(typectx, tokens)?
})
}
Ok(LTIRToken::Symbol(_)) => {
Ok(VariableBinding::Atomic{
symbol: parse_symbol(tokens)?,
typtag: parse_type_tag(typectx, tokens)?
})
}
Err(err) => Err(ParseError::LexError(err.clone())),
_ => Err(ParseError::UnexpectedToken)
}
} else {
Err(ParseError::UnexpectedEnd)
}
}
/* parse a block of symbol bidnings
* `{ x:T; y:U; ... }`
*/
pub fn parse_binding_block<It>(
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
tokens: &mut Peekable<LTIRLexer<It>>,
) -> Result< Vec<VariableBinding>, ParseError>
where
It: Iterator<Item = char>,
{
let _ = parse_expect(tokens, LTIRToken::BlockOpen)?;
let mut bindings = Vec::new();
while let Some((region, peektok)) = tokens.peek() {
match peektok {
Ok(LTIRToken::BlockClose) => {
tokens.next();
return Ok(bindings);
}
Ok(LTIRToken::StatementSep) => {
tokens.next();
}
Ok(_) => {
bindings.push(parse_binding_expr(typectx, tokens)?);
}
Err(err) => {
return Err(ParseError::LexError(err.clone()));
}
}
}
Err(ParseError::UnexpectedEnd)
}
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>>,
@ -79,7 +178,7 @@ pub fn parse_statement<It>(
where where
It: Iterator<Item = char>, It: Iterator<Item = char>,
{ {
if let Some(peektok) = tokens.peek() { if let Some((region, peektok)) = tokens.peek() {
match peektok { match peektok {
Ok(LTIRToken::Symbol(sym)) => { Ok(LTIRToken::Symbol(sym)) => {
match sym.as_str() { match sym.as_str() {
@ -98,13 +197,19 @@ where
"let" => { "let" => {
tokens.next(); tokens.next();
let name = parse_symbol(tokens)?; let name = parse_symbol(tokens)?;
let typ = parse_type_tag(typectx, tokens); let typ = parse_type_tag(typectx, tokens)?;
/* todo
let mut variable_bindings = parse_binding_expr(typectx, tokens)?;
*/
let _ = parse_expect(tokens, LTIRToken::AssignValue); let _ = parse_expect(tokens, LTIRToken::AssignValue);
let val_expr = parse_expr(typectx, tokens)?; let val_expr = parse_expr(typectx, tokens)?;
let _ = parse_expect(tokens, LTIRToken::StatementSep)?; let _ = parse_expect(tokens, LTIRToken::StatementSep)?;
Ok(Statement::LetAssign { Ok(Statement::LetAssign {
typ, typ: match typ {
Some(t) => Some(Ok(t)),
None => None
},
var_id: name, var_id: name,
val_expr, val_expr,
}) })
@ -116,7 +221,7 @@ where
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_statement_block(typectx, tokens)?,
}) })
} }
"return" => { "return" => {
@ -144,7 +249,7 @@ where
} }
} }
pub fn parse_block<It>( pub fn parse_statement_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>
@ -154,7 +259,7 @@ where
let _ = parse_expect(tokens, LTIRToken::BlockOpen)?; let _ = parse_expect(tokens, LTIRToken::BlockOpen)?;
let mut statements = Vec::new(); let mut statements = Vec::new();
while let Some(peektok) = tokens.peek() { while let Some((region, peektok)) = tokens.peek() {
match peektok { match peektok {
Ok(LTIRToken::BlockClose) => { Ok(LTIRToken::BlockClose) => {
tokens.next(); tokens.next();
@ -179,11 +284,11 @@ where
It: Iterator<Item = char>, It: Iterator<Item = char>,
{ {
match tokens.next() { match tokens.next() {
Some(Ok(LTIRToken::Symbol(sym))) => Ok(LTExpr::symbol(sym.as_str())), Some((region, Ok(LTIRToken::Symbol(sym)))) => Ok(LTExpr::symbol(sym.as_str())),
Some(Ok(LTIRToken::Char(c))) => Ok(LTExpr::lit_uint(c as u64)), Some((region, Ok(LTIRToken::Char(c)))) => Ok(LTExpr::lit_uint(c as u64)),
Some(Ok(LTIRToken::Num(n))) => Ok(LTExpr::lit_uint(n as u64)), Some((region, Ok(LTIRToken::Num(n)))) => Ok(LTExpr::lit_uint(n as u64)),
Some(Ok(_)) => Err(ParseError::UnexpectedToken), Some((region, Ok(_))) => Err(ParseError::UnexpectedToken),
Some(Err(err)) => Err(ParseError::LexError(err)), Some((region, Err(err))) => Err(ParseError::LexError(err)),
None => Err(ParseError::UnexpectedEnd), None => Err(ParseError::UnexpectedEnd),
} }
} }
@ -197,22 +302,18 @@ where
{ {
let mut children = Vec::new(); let mut children = Vec::new();
while let Some(tok) = tokens.peek() { while let Some((region, tok)) = tokens.peek() {
match tok { match tok {
Ok(LTIRToken::Lambda) => { Ok(LTIRToken::Lambda) => {
if children.len() == 0 { if children.len() == 0 {
tokens.next(); tokens.next();
let mut args = Vec::new(); let mut variable_bindings = parse_binding_expr(typectx, tokens)?;
while let Some(Ok(LTIRToken::Symbol(_))) = tokens.peek() {
args.push((parse_symbol(tokens)?, parse_type_tag(typectx, tokens)));
}
let _ = parse_expect(tokens, LTIRToken::LambdaBody); let _ = parse_expect(tokens, LTIRToken::LambdaBody);
let body = parse_expr(typectx, tokens)?; let body = parse_expr(typectx, tokens)?;
return Ok(LTExpr::Abstraction { return Ok(LTExpr::Abstraction {
args, args: variable_bindings.flatten().into_iter().map(|(s,t)| (s,t.map(|t|Ok(t))) ).collect(),
body: Box::new(body), body: Box::new(body),
}); });
} else { } else {
@ -221,7 +322,7 @@ where
} }
Ok(LTIRToken::ExprOpen) => { Ok(LTIRToken::ExprOpen) => {
tokens.next(); tokens.next();
while let Some(peektok) = tokens.peek() { while let Some((region, peektok)) = tokens.peek() {
match peektok { match peektok {
Ok(LTIRToken::ExprClose) => { Ok(LTIRToken::ExprClose) => {
tokens.next(); tokens.next();
@ -236,7 +337,7 @@ where
break; break;
} }
Ok(LTIRToken::BlockOpen) => { Ok(LTIRToken::BlockOpen) => {
children.push(LTExpr::block(parse_block(typectx, tokens)?)); children.push(LTExpr::block(parse_statement_block(typectx, tokens)?));
} }
Ok(LTIRToken::BlockClose) => { Ok(LTIRToken::BlockClose) => {
break; break;
@ -250,10 +351,10 @@ where
let _ = parse_expect(tokens, LTIRToken::ExprOpen)?; let _ = parse_expect(tokens, LTIRToken::ExprOpen)?;
let cond = parse_expr(typectx, tokens)?; let cond = parse_expr(typectx, tokens)?;
let _ = parse_expect(tokens, LTIRToken::ExprClose)?; let _ = parse_expect(tokens, LTIRToken::ExprClose)?;
let if_expr = LTExpr::block(parse_block(typectx, tokens)?); let if_expr = LTExpr::block(parse_statement_block(typectx, tokens)?);
let mut else_expr = LTExpr::block(vec![]); let mut else_expr = LTExpr::block(vec![]);
if let Some(peektok) = tokens.peek() { if let Some((region, peektok)) = tokens.peek() {
if let Ok(LTIRToken::Symbol(name)) = peektok { if let Ok(LTIRToken::Symbol(name)) = peektok {
if name == "else" { if name == "else" {
tokens.next(); tokens.next();
@ -292,3 +393,99 @@ where
Err(ParseError::UnexpectedEnd) Err(ParseError::UnexpectedEnd)
} }
} }
mod tests {
use std::sync::{Arc, RwLock};
#[test]
fn test_parse_atomic_binding() {
let mut lexer = crate::lexer::LTIRLexer::from("x".chars()).peekable();
let typectx = Arc::new(RwLock::new(laddertypes::dict::TypeDict::new()));
let bindings = crate::parser::parse_binding_expr( &typectx, &mut lexer );
assert_eq!(
bindings,
Ok(crate::parser::VariableBinding::Atomic{
symbol: "x".into(),
typtag: None
})
);
}
#[test]
fn test_parse_typed_atomic_binding() {
let mut lexer = crate::lexer::LTIRLexer::from("x:T".chars()).peekable();
let typectx = Arc::new(RwLock::new(laddertypes::dict::TypeDict::new()));
let bindings = crate::parser::parse_binding_expr( &typectx, &mut lexer );
assert_eq!(
bindings,
Ok(crate::parser::VariableBinding::Atomic{
symbol: "x".into(),
typtag: Some(typectx.write().unwrap().parse("T").unwrap())
})
);
}
#[test]
fn test_parse_struct_binding() {
let mut lexer = crate::lexer::LTIRLexer::from("{x y}".chars()).peekable();
let typectx = Arc::new(RwLock::new(laddertypes::dict::TypeDict::new()));
let bindings = crate::parser::parse_binding_expr( &typectx, &mut lexer );
assert_eq!(
bindings,
Ok(crate::parser::VariableBinding::Struct{
members: vec![
crate::parser::VariableBinding::Atomic{ symbol: "x".into(), typtag: None },
crate::parser::VariableBinding::Atomic{ symbol: "y".into(), typtag: None }
],
typtag: None
})
);
}
#[test]
fn test_parse_typed_struct_binding1() {
let mut lexer = crate::lexer::LTIRLexer::from("{x y}:T".chars()).peekable();
let typectx = Arc::new(RwLock::new(laddertypes::dict::TypeDict::new()));
let bindings = crate::parser::parse_binding_expr( &typectx, &mut lexer );
assert_eq!(
bindings,
Ok(crate::parser::VariableBinding::Struct{
members: vec![
crate::parser::VariableBinding::Atomic{ symbol: "x".into(), typtag: None },
crate::parser::VariableBinding::Atomic{ symbol: "y".into(), typtag: None }
],
typtag: Some(typectx.write().unwrap().parse("T").unwrap())
})
);
}
#[test]
fn test_parse_typed_struct_binding2() {
let mut lexer = crate::lexer::LTIRLexer::from("{x:U; y}:T".chars()).peekable();
let typectx = Arc::new(RwLock::new(laddertypes::dict::TypeDict::new()));
let bindings = crate::parser::parse_binding_expr( &typectx, &mut lexer );
let type_u = typectx.write().unwrap().parse("U").unwrap();
let type_t = typectx.write().unwrap().parse("T").unwrap();
assert_eq!(
bindings,
Ok(crate::parser::VariableBinding::Struct{
members: vec![
crate::parser::VariableBinding::Atomic{
symbol: "x".into(),
typtag: Some(type_u)
},
crate::parser::VariableBinding::Atomic{ symbol: "y".into(), typtag: None }
],
typtag: Some(type_t)
})
);
}
}

View file

@ -18,13 +18,14 @@ pub fn init_runtime(linker: &mut Linker) -> Arc<RwLock<Scope>> {
vec!["T~machine::Word", "T~machine::Word"], vec!["T~machine::Word", "T~machine::Word"],
); );
linker.add_procedure( /* drop topmost element
"dup", */
tisc::Assembler::new() symbols.write().unwrap().declare_proc_parse(
.inst(tisc::VM_Instruction::Dup) "drop",
.build(), vec!["T"],
vec!["T~machine::Word"],
vec![],
); );
/* Put a single Ascii character on stdout /* Put a single Ascii character on stdout
*/ */
symbols.write().unwrap().declare_proc_parse( symbols.write().unwrap().declare_proc_parse(
@ -33,14 +34,18 @@ pub fn init_runtime(linker: &mut Linker) -> Arc<RwLock<Scope>> {
vec!["Char~Ascii~machine::Word"], vec!["Char~Ascii~machine::Word"],
vec![], vec![],
); );
symbols.write().unwrap().declare_proc_parse(
linker.add_procedure( "accept",
"emit", vec![],
tisc::Assembler::new() vec![],
.inst(tisc::VM_Instruction::Emit) vec!["Char~Ascii~machine::Word"],
.build(),
); );
linker.add_procedure("dup", tisc::Assembler::new().inst(tisc::VM_Instruction::Dup).build());
linker.add_procedure("drop", tisc::Assembler::new().inst(tisc::VM_Instruction::Accept).build());
linker.add_procedure("emit", tisc::Assembler::new().inst(tisc::VM_Instruction::Emit).build());
linker.add_procedure("accept", tisc::Assembler::new().inst(tisc::VM_Instruction::Accept).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.
* We do not know wheter a sum of two integers actually * We do not know wheter a sum of two integers actually
@ -55,16 +60,43 @@ pub fn init_runtime(linker: &mut Linker) -> Arc<RwLock<Scope>> {
], ],
vec!["_2^64~machine::UInt64~machine::Word"], vec!["_2^64~machine::UInt64~machine::Word"],
); );
symbols.write().unwrap().declare_proc_parse(
linker.add_procedure( "i-",
"i+", vec![],
tisc::Assembler::new() vec![
.inst(tisc::VM_Instruction::IntAdd) "_2^64~machine::UInt64~machine::Word",
.build(), "_2^64~machine::UInt64~machine::Word",
],
vec!["_2^64~machine::UInt64~machine::Word"],
);
symbols.write().unwrap().declare_proc_parse(
"i*",
vec![],
vec![
"_2^64~machine::UInt64~machine::Word",
"_2^64~machine::UInt64~machine::Word",
],
vec!["_2^64~machine::UInt64~machine::Word"],
);
symbols.write().unwrap().declare_proc_parse(
"i/",
vec![],
vec![
"_2^64~machine::UInt64~machine::Word",
"_2^64~machine::UInt64~machine::Word",
],
vec!["_2^64~machine::UInt64~machine::Word"],
);
symbols.write().unwrap().declare_proc_parse(
"i%",
vec![],
vec![
"_2^64~machine::UInt64~machine::Word",
"_2^64~machine::UInt64~machine::Word",
],
vec!["_2^64~machine::UInt64~machine::Word"],
); );
/* Floating-point Addition
*/
symbols.write().unwrap().declare_proc_parse( symbols.write().unwrap().declare_proc_parse(
"f+", "f+",
vec![], vec![],
@ -75,35 +107,93 @@ pub fn init_runtime(linker: &mut Linker) -> Arc<RwLock<Scope>> {
vec!["~machine::f64~machine::Word"], vec!["~machine::f64~machine::Word"],
); );
linker.add_procedure(
"f+",
tisc::Assembler::new()
.inst(tisc::VM_Instruction::FltAdd)
.build(),
);
/* Integer Subtraction
*/
symbols.write().unwrap().declare_proc_parse( symbols.write().unwrap().declare_proc_parse(
"i-", "f-",
vec![], vec![],
vec![ vec![
"_2^64~machine::UInt64~machine::Word", "~machine::f64~machine::Word",
"_2^64~machine::UInt64~machine::Word", "~machine::f64~machine::Word",
], ],
vec!["_2^64~machine::UInt64~machine::Word"], vec!["~machine::f64~machine::Word"],
); );
linker.add_procedure(
"i-", symbols.write().unwrap().declare_proc_parse(
tisc::Assembler::new() "f*",
.inst(tisc::VM_Instruction::Swap) vec![],
.inst(tisc::VM_Instruction::BitNeg) vec![
.lit(1) "~machine::f64~machine::Word",
.inst(tisc::VM_Instruction::IntAdd) "~machine::f64~machine::Word",
.inst(tisc::VM_Instruction::IntAdd) ],
.build(), vec!["~machine::f64~machine::Word"],
); );
symbols.write().unwrap().declare_proc_parse(
"f/",
vec![],
vec![
"~machine::f64~machine::Word",
"~machine::f64~machine::Word",
],
vec!["~machine::f64~machine::Word"],
);
symbols.write().unwrap().declare_proc_parse(
"f%",
vec![],
vec![
"~machine::f64~machine::Word",
"~machine::f64~machine::Word",
],
vec!["~machine::f64~machine::Word"],
);
linker.add_procedure("i+", tisc::Assembler::new().inst(tisc::VM_Instruction::IntAdd).build());
linker.add_procedure("i-", tisc::Assembler::new().inst(tisc::VM_Instruction::IntSub).build());
linker.add_procedure("i*", tisc::Assembler::new().inst(tisc::VM_Instruction::IntMul).build());
linker.add_procedure("i/", tisc::Assembler::new().inst(tisc::VM_Instruction::IntDiv).build());
linker.add_procedure("i%", tisc::Assembler::new().inst(tisc::VM_Instruction::IntRem).build());
linker.add_procedure("f+", tisc::Assembler::new().inst(tisc::VM_Instruction::FltAdd).build());
linker.add_procedure("f-", tisc::Assembler::new().inst(tisc::VM_Instruction::FltSub).build());
linker.add_procedure("f*", tisc::Assembler::new().inst(tisc::VM_Instruction::FltMul).build());
linker.add_procedure("f/", tisc::Assembler::new().inst(tisc::VM_Instruction::FltDiv).build());
linker.add_procedure("f%", tisc::Assembler::new().inst(tisc::VM_Instruction::FltRem).build());
symbols.write().unwrap().declare_proc_parse(
"bit-neg",
vec![], vec!["machine::Word", "machine::Word"], vec!["machine::Word"],
);
symbols.write().unwrap().declare_proc_parse(
"bit-and",
vec![], vec!["machine::Word", "machine::Word"], vec!["machine::Word"],
);
symbols.write().unwrap().declare_proc_parse(
"bit-or",
vec![], vec!["machine::Word", "machine::Word"], vec!["machine::Word"],
);
symbols.write().unwrap().declare_proc_parse(
"bit-xor",
vec![], vec!["machine::Word", "machine::Word"], vec!["machine::Word"],
);
symbols.write().unwrap().declare_proc_parse(
"bit-shl",
vec![], vec!["machine::Word", "machine::Word"], vec!["machine::Word"],
);
symbols.write().unwrap().declare_proc_parse(
"bit-shr",
vec![], vec!["machine::Word", "machine::Word"], vec!["machine::Word"],
);
linker.add_procedure("bit-neg", tisc::Assembler::new().inst(tisc::VM_Instruction::BitNeg).build());
linker.add_procedure("bit-and", tisc::Assembler::new().inst(tisc::VM_Instruction::BitAnd).build());
linker.add_procedure("bit-or", tisc::Assembler::new().inst(tisc::VM_Instruction::BitOr).build());
linker.add_procedure("bit-xor", tisc::Assembler::new().inst(tisc::VM_Instruction::BitXor).build());
linker.add_procedure("bit-shl", tisc::Assembler::new().inst(tisc::VM_Instruction::BitShl).build());
linker.add_procedure("bit-shr", tisc::Assembler::new().inst(tisc::VM_Instruction::BitShr).build());
/* Fetch memory address /* Fetch memory address
*/ */
symbols.write().unwrap().declare_proc_parse( symbols.write().unwrap().declare_proc_parse(
@ -112,13 +202,6 @@ pub fn init_runtime(linker: &mut Linker) -> Arc<RwLock<Scope>> {
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(
"@",
tisc::Assembler::new()
.inst(tisc::VM_Instruction::Fetch)
.build(),
);
/* Store to memory /* Store to memory
*/ */
symbols.write().unwrap().declare_proc_parse( symbols.write().unwrap().declare_proc_parse(
@ -130,72 +213,11 @@ pub fn init_runtime(linker: &mut Linker) -> Arc<RwLock<Scope>> {
], ],
vec![], vec![],
); );
linker.add_procedure(
"!",
tisc::Assembler::new()
.inst(tisc::VM_Instruction::Store)
.build(),
);
/* linker.add_procedure("@", tisc::Assembler::new().inst(tisc::VM_Instruction::Fetch).build());
* let muli = λa.λb.{ linker.add_procedure("!", tisc::Assembler::new().inst(tisc::VM_Instruction::Store).build());
* let mut sum = 0;
* while( b != 0 ) {
* sum := (addi sum a);
* b := (subi b 1);
* }
* sum
* };
*/
symbols.write().unwrap().declare_proc_parse(
"i*",
vec![],
vec![
"_2^64~machine::UInt64~machine::Word",
"_2^64~machine::UInt64~machine::Word",
],
vec!["_2^64~machine::UInt64~machine::Word"],
);
linker.add_procedure(
"i*",
tisc::Assembler::new()
.lit(0) // [ a b ] -- [ a b sum ]
.while_loop(
// condition
tisc::Assembler::new()
// [ a b sum ] -- [ a b sum b ]
.lit(2)
.inst(tisc::VM_Instruction::Pick),
// body
tisc::Assembler::new()
// [ a b sum ] -- [ a b sum a ]
.lit(3)
.inst(tisc::VM_Instruction::Pick)
// [ a b sum a -- a b (sum+a) ]
.inst(tisc::VM_Instruction::IntAdd)
// [ a b sum -- a sum b ]
.inst(tisc::VM_Instruction::Swap)
// [ a sum b -- a sum b 1 ]
.lit(1)
// [ a sum b -- a sum (b-1) ]
.inst(tisc::VM_Instruction::Swap)
.call("i-")
// [ a sum b -- a b sum ]
.inst(tisc::VM_Instruction::Swap),
)
// [ a b sum -- a sum b ]
.lit(2)
.inst(tisc::VM_Instruction::Roll)
// [ a sum b -- a sum ]
.inst(tisc::VM_Instruction::Drop)
// [ a sum -- sum a ]
.lit(2)
.inst(tisc::VM_Instruction::Roll)
// [ sum a -- sum ]
.inst(tisc::VM_Instruction::Drop)
.build(),
);
symbols.write().unwrap().declare_static_parse( symbols.write().unwrap().declare_static_parse(
"data-frame-ptr", "data-frame-ptr",