Skip to content
Snippets Groups Projects
Commit 5fa27330 authored by Stephen D's avatar Stephen D
Browse files

fixes and some examples

parent c713c36d
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,12 @@ ...@@ -2,6 +2,12 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 4 version = 4
[[package]]
name = "anyhow"
version = "1.0.97"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcfed56ad506cb2c684a14971b8861fdc3baaaae314b9e5f9bb532cbe3ba7a4f"
[[package]] [[package]]
name = "bitvec" name = "bitvec"
version = "1.0.1" version = "1.0.1"
...@@ -30,6 +36,7 @@ checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" ...@@ -30,6 +36,7 @@ checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09"
name = "sophiescript" name = "sophiescript"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"bitvec", "bitvec",
] ]
......
...@@ -4,4 +4,5 @@ version = "0.1.0" ...@@ -4,4 +4,5 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
anyhow = "1.0.97"
bitvec = "1.0.1" bitvec = "1.0.1"
$false = λtf.f
$true = λtf.t
λa.(a $true)
$true = λtf.t;
$false = λtf.f;
# λλλ [[0 2] 1]
$pair = λabc.cab;
$0 = $pair $true;
$1 = $pair $false;
$H = ($pair ($0 ($1 ($0 ($0 ($1 ($0 ($0 ($0 $false)))))))));
$e = ($pair ($0 ($1 ($1 ($0 ($0 ($1 ($0 ($1 $false)))))))));
$l = ($pair ($0 ($1 ($1 ($0 ($1 ($1 ($0 ($0 $false)))))))));
$o = ($pair ($0 ($1 ($1 ($0 ($1 ($1 ($1 ($1 $false)))))))));
$sp = ($pair ($0 ($0 ($1 ($0 ($0 ($0 ($0 ($0 $false)))))))));
$W = ($pair ($0 ($1 ($0 ($1 ($0 ($1 ($1 ($1 $false)))))))));
$r = ($pair ($0 ($1 ($1 ($1 ($0 ($0 ($1 ($0 $false)))))))));
$d = ($pair ($0 ($1 ($1 ($0 ($0 ($1 ($0 ($0 $false)))))))));
$nl = ($pair ($0 ($0 ($0 ($0 ($1 ($0 ($1 ($0 $false)))))))));
$exc = ($pair ($0 ($0 ($1 ($0 ($0 ($0 ($0 ($1 $false)))))))));
λa. $H ($e ($l ($l ($o ($sp ($W ($o ($r ($l ($d ($exc ($nl $false))))))))))));
\ No newline at end of file
$false=λtf.f;
$omega=λa. (a a);
λa.a($omega(λbcde.d(bb)(λf.fce)))$false;
λa.(λb.bb)(λb.(λcde.e(λfg.(λh.hh)(λhi.i(λjkl.hhk(λm.j(λnopqrs.sm(n(λu.uoq)q)(nr(λu.uor)))(λnop.p(λqr.mq(λs.sqr))no)))(λj.(λk.jkkk)(λkl.l)))e(λhijk.(λl.h(d(λmn.n))(c(l(λmn.m))i(c(l(λmn.n))jk)))(λlm.d(λn.nlm)))))(bb))(λb.b)a(λbc.c);
\ No newline at end of file
use std::{fs, io::Write};
use bitvec::vec::BitVec; use bitvec::vec::BitVec;
use flatten::Flattener; use flatten::Flattener;
use parser::Parser; use parser::Parser;
...@@ -10,34 +12,38 @@ mod scanner; ...@@ -10,34 +12,38 @@ mod scanner;
mod stmt; mod stmt;
mod token; mod token;
fn main() { fn main() -> anyhow::Result<()> {
let scanner = Scanner::new( let args: Vec<_> = std::env::args().collect();
" if args.len() != 2 {
$false=λtf.f; println!("Usage: {} filename.sos", args[0]);
$omega=λa. (a a); return Ok(());
}
let code = fs::read_to_string(&args[1])?;
run(&code);
Ok(())
}
(λa.a($omega(λbcde.d(bb)(λf.fce)))$false); fn run(code: &str) -> Option<()> {
#(λabc.cb(ab)); let scanner = Scanner::new(code);
#(λabc.cba);
",
// "((λab.ba)(λa.a))",
);
let tokens = scanner.scan_tokens().unwrap(); let tokens = scanner.scan_tokens()?;
let mut parser = Parser::new(tokens); let mut parser = Parser::new(tokens);
let (funcs, main) = parser.parse().unwrap(); let (funcs, main) = parser.parse()?;
let mut flattener = Flattener::new(funcs); let mut flattener = Flattener::new(funcs);
let flat = flattener.flatten(&main).unwrap(); let flat = flattener.flatten(&main)?;
println!("{flat}");
/*let mut bv = BitVec::new(); let mut bv = BitVec::new();
flat.to_blc(&mut bv); flat.to_blc(&mut bv);
let mut stdout = std::io::stdout();
for x in bv.as_raw_slice() { for x in bv.as_raw_slice() {
print!("{}", *x as char); stdout.write_all(&[*x]).unwrap();
}*/ }
Some(())
} }
...@@ -87,8 +87,6 @@ impl Parser { ...@@ -87,8 +87,6 @@ impl Parser {
fn expr(&mut self) -> Option<Expr> { fn expr(&mut self) -> Option<Expr> {
let mut expr = self.expr_once()?; let mut expr = self.expr_once()?;
dbg!(&expr);
while !self.is_at_end_of_expr() { while !self.is_at_end_of_expr() {
let right = self.expr_once()?; let right = self.expr_once()?;
expr = Expr::App(Box::new(expr), Box::new(right)); expr = Expr::App(Box::new(expr), Box::new(right));
...@@ -113,8 +111,6 @@ impl Parser { ...@@ -113,8 +111,6 @@ impl Parser {
let expr = self.expr()?; let expr = self.expr()?;
dbg!(&self.peek().token_type);
if self.peek().token_type != TokenType::RightParen { if self.peek().token_type != TokenType::RightParen {
self.print_error("Expected ')'"); self.print_error("Expected ')'");
return None; return None;
...@@ -133,8 +129,6 @@ impl Parser { ...@@ -133,8 +129,6 @@ impl Parser {
} }
_ => { _ => {
dbg!(&self.peek().token_type);
self.print_error( self.print_error(
"Unexpected token. Expected one of 'λ', '/', '\\', '$', '(', or a variable", "Unexpected token. Expected one of 'λ', '/', '\\', '$', '(', or a variable",
); );
...@@ -169,8 +163,6 @@ impl Parser { ...@@ -169,8 +163,6 @@ impl Parser {
} }
fn find_variable(&self, name: char) -> Option<usize> { fn find_variable(&self, name: char) -> Option<usize> {
dbg!(name, &self.env);
let ret = self let ret = self
.env .env
.iter() .iter()
......
...@@ -20,7 +20,7 @@ impl Scanner { ...@@ -20,7 +20,7 @@ impl Scanner {
} }
} }
pub fn scan_tokens(mut self) -> Result<Vec<Token>, ()> { pub fn scan_tokens(mut self) -> Option<Vec<Token>> {
while !self.is_at_end() { while !self.is_at_end() {
self.start = self.current; self.start = self.current;
self.scan_token()?; self.scan_token()?;
...@@ -28,10 +28,10 @@ impl Scanner { ...@@ -28,10 +28,10 @@ impl Scanner {
self.add_token(TokenType::Eof); self.add_token(TokenType::Eof);
Ok(self.tokens) Some(self.tokens)
} }
pub fn scan_token(&mut self) -> Result<(), ()> { pub fn scan_token(&mut self) -> Option<()> {
use TokenType::*; use TokenType::*;
let c = self.advance(); let c = self.advance();
...@@ -51,7 +51,7 @@ impl Scanner { ...@@ -51,7 +51,7 @@ impl Scanner {
} }
'#' => { '#' => {
// comment // comment
while self.peek() != '\n' { while !self.is_at_end() && self.peek() != '\n' {
self.advance(); self.advance();
} }
...@@ -61,31 +61,31 @@ impl Scanner { ...@@ -61,31 +61,31 @@ impl Scanner {
_ => { _ => {
self.print_error(&format!("Unexpected character '{}'.", c)); self.print_error(&format!("Unexpected character '{}'.", c));
return Err(()); return None;
} }
}; };
self.add_token(token_type); self.add_token(token_type);
Ok(()) Some(())
} }
fn definition(&mut self) -> Result<(), ()> { fn definition(&mut self) -> Option<()> {
while self.peek().is_alphanumeric() { while !self.is_at_end() && self.peek().is_alphanumeric() {
self.advance(); self.advance();
} }
if self.start + 1 == self.current { if self.start + 1 == self.current {
self.print_error("Missing function name after $"); self.print_error("Missing function name after $");
return Err(()); return None;
} }
self.add_token(TokenType::DefinitionName( self.add_token(TokenType::DefinitionName(
self.chars[(self.start + 1)..self.current].iter().collect(), self.chars[(self.start + 1)..self.current].iter().collect(),
)); ));
Ok(()) Some(())
} }
fn add_token(&mut self, token_type: TokenType) { fn add_token(&mut self, token_type: TokenType) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment