Compare commits
No commits in common. "bf3e784b423ac948930b8e611f955af8914220dadeb9ce9b4b4ad34780df6fd8" and "66e7ab5b219d0961fb4032c7a57c822c2a2a29b092a2f19930427af98f559cc8" have entirely different histories.
bf3e784b42
...
66e7ab5b21
5 changed files with 99 additions and 183 deletions
142
src/executor.rs
142
src/executor.rs
|
@ -1,6 +1,6 @@
|
|||
use std::{collections::HashMap, fmt};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::PreLispValue;
|
||||
use crate::{OpCode, LispValue};
|
||||
|
||||
pub struct LispState {
|
||||
table: HashMap<String, LispValue>,
|
||||
|
@ -10,54 +10,6 @@ fn add_function<T: Into<String> + Clone>(table: &mut HashMap<String, LispValue>,
|
|||
table.insert(name.clone().into(), LispValue::RustFunction(name.into(), func));
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum LispValue {
|
||||
Nil,
|
||||
String(String),
|
||||
Integer(i32),
|
||||
Float(f32),
|
||||
List(Vec<LispValue>, bool),
|
||||
LispFunction(String, Vec<LispValue>),
|
||||
RustFunction(String, fn(Vec<LispValue>) -> Vec<LispValue>),
|
||||
}
|
||||
|
||||
impl fmt::Display for LispValue {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
LispValue::Nil => write!(f, "nil"),
|
||||
LispValue::String(str) => write!(f, "{}", str),
|
||||
LispValue::LispFunction(name, _) => write!(f, "<'{}': Lisp Function>", name),
|
||||
LispValue::RustFunction(name, _) => write!(f, "<'{}': Rust Function>", name),
|
||||
LispValue::Integer(num) => write!(f, "{}", num),
|
||||
LispValue::Float(num) => write!(f, "{}", num),
|
||||
val => {
|
||||
write!(f, "{}", format!("{:?}", val))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for LispValue {
|
||||
fn from(value: &str) -> Self {
|
||||
LispValue::String(value.to_string())
|
||||
}
|
||||
}
|
||||
impl From<String> for LispValue {
|
||||
fn from(value: String) -> Self {
|
||||
LispValue::String(value)
|
||||
}
|
||||
}
|
||||
impl From<i32> for LispValue {
|
||||
fn from(value: i32) -> Self {
|
||||
LispValue::Integer(value)
|
||||
}
|
||||
}
|
||||
impl From<f32> for LispValue {
|
||||
fn from(value: f32) -> Self {
|
||||
LispValue::Float(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl LispState {
|
||||
pub fn new() -> LispState {
|
||||
let mut table = HashMap::new();
|
||||
|
@ -329,73 +281,47 @@ impl LispState {
|
|||
}
|
||||
}
|
||||
|
||||
fn handle_prevalues(&self, input: Vec<PreLispValue>) -> Vec<LispValue> {
|
||||
let mut out = Vec::new();
|
||||
fn do_call(&self, opcode: &OpCode) -> Vec<LispValue> {
|
||||
if let OpCode::Call(func, args) = opcode {
|
||||
let f = self.table.get(func).unwrap();
|
||||
|
||||
for v in input {
|
||||
out.push(match v {
|
||||
PreLispValue::Nil => LispValue::Nil,
|
||||
PreLispValue::String(str) => LispValue::String(str),
|
||||
PreLispValue::Integer(num) => LispValue::Integer(num),
|
||||
PreLispValue::Float(num) => LispValue::Float(num),
|
||||
PreLispValue::List(list, quoted) => {
|
||||
LispValue::List(Self::handle_prevalues(self, list), quoted)
|
||||
},
|
||||
PreLispValue::LispFunction(name, instructions) => {
|
||||
LispValue::LispFunction(name, Self::handle_prevalues(self, instructions))
|
||||
},
|
||||
PreLispValue::Var(name) => {
|
||||
self.table.get(&name).unwrap_or(&LispValue::Nil).to_owned()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
fn eval_list(list: &Vec<LispValue>, quoted: bool) -> (Vec<LispValue>, bool) {
|
||||
if list.len() < 1 {
|
||||
return (vec![LispValue::List(Vec::new(), quoted)], false);
|
||||
}
|
||||
|
||||
let list = &mut list.clone();
|
||||
|
||||
for i in 0..list.len() {
|
||||
if let LispValue::List(elements, quoted) = &list[i] {
|
||||
let (elems, astuple) = Self::eval_list(elements, *quoted);
|
||||
if astuple {
|
||||
list[i] = elems.get(0).unwrap_or(&LispValue::Nil).to_owned();
|
||||
for j in 1..elems.len() {
|
||||
list.insert(i, elems[j].to_owned());
|
||||
let mut args: Vec<LispValue> = args.clone();
|
||||
for i in 0..args.len() {
|
||||
let arg = &args[i];
|
||||
if let LispValue::Eval(OpCode::Exp(opcodes)) = arg {
|
||||
if opcodes.len() < 1 {
|
||||
args[i] = LispValue::Nil;
|
||||
continue;
|
||||
}
|
||||
|
||||
let call_opcode = opcodes.last().unwrap();
|
||||
let mut res = self.do_call(call_opcode);
|
||||
args.remove(i);
|
||||
for j in 0..res.len() {
|
||||
args.insert(i + j, res.remove(0));
|
||||
}
|
||||
} else {
|
||||
list[i] = LispValue::List(elems, *quoted);
|
||||
}
|
||||
}
|
||||
}
|
||||
if let LispValue::RustFunction(_, f) = f {
|
||||
return f(args.to_vec());
|
||||
} else {
|
||||
todo!();
|
||||
}
|
||||
|
||||
if quoted {
|
||||
return (list.to_vec(), false);
|
||||
}
|
||||
|
||||
if let LispValue::RustFunction(_name, f) = &list[0] {
|
||||
(f(list[1..].to_vec()), true)
|
||||
} else if let LispValue::LispFunction(_name, ins) = &list[0] {
|
||||
(Self::call_lisp_func(&list[0], list[1..].to_vec()), true)
|
||||
} else {
|
||||
(list.to_vec(), false)
|
||||
panic!("Bad OpCode: expected OpCode::Call")
|
||||
}
|
||||
}
|
||||
|
||||
fn call_lisp_func(f: &LispValue, args: Vec<LispValue>) -> Vec<LispValue> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn execute(&self, instructions: Vec<PreLispValue>) {
|
||||
let instructions = Self::handle_prevalues(self, instructions);
|
||||
for val in instructions {
|
||||
if let LispValue::List(elements, quoted) = val {
|
||||
Self::eval_list(&elements, quoted);
|
||||
pub fn execute(&self, instructions: Vec<OpCode>) {
|
||||
for op in instructions {
|
||||
match op {
|
||||
OpCode::Call(func, args) => {
|
||||
self.do_call(&OpCode::Call(func, args));
|
||||
},
|
||||
OpCode::Exp(ins) => {
|
||||
self.execute(ins);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@ use executor::*;
|
|||
|
||||
fn main() {
|
||||
let source = std::fs::read_to_string("src/test.lisp").unwrap();
|
||||
|
||||
let mut tokenizer = Tokenizer::new(source);
|
||||
let tokens = match tokenizer.tokenize() {
|
||||
Ok(tokens) => tokens,
|
||||
|
|
131
src/parser.rs
131
src/parser.rs
|
@ -3,53 +3,54 @@ use std::fmt;
|
|||
use crate::Token;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum PreLispValue {
|
||||
pub enum LispValue {
|
||||
Nil,
|
||||
String(String),
|
||||
Var(String),
|
||||
Integer(i32),
|
||||
Float(f32),
|
||||
List(Vec<PreLispValue>, bool), // Values, Quoted
|
||||
LispFunction(String, Vec<PreLispValue>),
|
||||
LispFunction(String, Vec<OpCode>),
|
||||
RustFunction(String, fn(Vec<LispValue>) -> Vec<LispValue>),
|
||||
Ref(String),
|
||||
Eval(OpCode)
|
||||
}
|
||||
|
||||
impl fmt::Display for PreLispValue {
|
||||
impl fmt::Display for LispValue {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
PreLispValue::Nil => write!(f, "nil"),
|
||||
PreLispValue::String(str) => write!(f, "{}", str),
|
||||
PreLispValue::LispFunction(name, _) => write!(f, "<'{}': Lisp Function>", name),
|
||||
PreLispValue::Integer(num) => write!(f, "{}", num),
|
||||
PreLispValue::Float(num) => write!(f, "{}", num),
|
||||
LispValue::Nil => write!(f, "nil"),
|
||||
LispValue::String(str) => write!(f, "{}", str),
|
||||
LispValue::LispFunction(name, _) => write!(f, "<'{}': Lisp Function>", name),
|
||||
LispValue::RustFunction(name, _) => write!(f, "<'{}': Rust Function>", name),
|
||||
LispValue::Integer(num) => write!(f, "{}", num),
|
||||
LispValue::Float(num) => write!(f, "{}", num),
|
||||
LispValue::Ref(name) => write!(f, "<'{}': Reference>", name),
|
||||
_ => todo!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for PreLispValue {
|
||||
impl From<&str> for LispValue {
|
||||
fn from(value: &str) -> Self {
|
||||
PreLispValue::String(value.to_string())
|
||||
LispValue::String(value.to_string())
|
||||
}
|
||||
}
|
||||
impl From<String> for PreLispValue {
|
||||
|
||||
impl From<String> for LispValue {
|
||||
fn from(value: String) -> Self {
|
||||
PreLispValue::String(value)
|
||||
LispValue::String(value)
|
||||
}
|
||||
}
|
||||
impl From<i32> for PreLispValue {
|
||||
|
||||
impl From<i32> for LispValue {
|
||||
fn from(value: i32) -> Self {
|
||||
PreLispValue::Integer(value)
|
||||
LispValue::Integer(value)
|
||||
}
|
||||
}
|
||||
impl From<f32> for PreLispValue {
|
||||
fn from(value: f32) -> Self {
|
||||
PreLispValue::Float(value)
|
||||
}
|
||||
}
|
||||
impl From<Vec<PreLispValue>> for PreLispValue {
|
||||
fn from(value: Vec<PreLispValue>) -> Self {
|
||||
PreLispValue::List(value, false)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum OpCode {
|
||||
Call(String, Vec<LispValue>),
|
||||
Exp(Vec<OpCode>)
|
||||
}
|
||||
|
||||
// TODO: Handle failure
|
||||
|
@ -82,64 +83,60 @@ fn read_exp(open_paren_idx: usize, tokens: &Vec<Token>) -> Option<(Vec<Token>, u
|
|||
None
|
||||
}
|
||||
|
||||
fn parse_exp(tokens: Vec<Token>, quoted: bool) -> PreLispValue {
|
||||
let mut values = Vec::new();
|
||||
fn parse_exp(tokens: Vec<Token>) -> Vec<OpCode> {
|
||||
let mut opcodes = Vec::new();
|
||||
|
||||
if tokens.len() < 1 {
|
||||
return PreLispValue::List(values, false);
|
||||
return opcodes;
|
||||
}
|
||||
|
||||
let mut quote_next_list = false;
|
||||
let mut unquote_next_list = false;
|
||||
if let Token::Identifier(name) = &tokens[0] {
|
||||
let mut args = Vec::new();
|
||||
|
||||
let mut i = 0;
|
||||
while i < tokens.len() {
|
||||
let tkn = &tokens[i];
|
||||
match tkn {
|
||||
Token::Quote => {
|
||||
quote_next_list = true;
|
||||
},
|
||||
Token::Unquote => {
|
||||
unquote_next_list = true;
|
||||
let mut i = 1;
|
||||
while i < tokens.len() {
|
||||
let tkn = &tokens[i];
|
||||
match tkn {
|
||||
Token::OpenParen => {
|
||||
let (tkns, close_paren_idx) = read_exp(i, &tokens).unwrap();
|
||||
args.push(LispValue::Eval(OpCode::Exp(parse_exp(tkns))));
|
||||
i = close_paren_idx;
|
||||
},
|
||||
Token::Identifier(name) => {
|
||||
args.push(LispValue::Ref(name.to_owned()));
|
||||
},
|
||||
Token::Integer(num) => {
|
||||
args.push(LispValue::Integer(*num));
|
||||
},
|
||||
Token::Float(num) => {
|
||||
args.push(LispValue::Float(*num));
|
||||
},
|
||||
Token::String(str) => {
|
||||
args.push(LispValue::String(str.to_owned()));
|
||||
},
|
||||
Token::CloseParen => {
|
||||
panic!("Unexpected closing parenthesis");
|
||||
},
|
||||
}
|
||||
Token::OpenParen => {
|
||||
let (tkns, close_paren_idx) = read_exp(i, &tokens).unwrap();
|
||||
values.push(parse_exp(tkns, if unquote_next_list {false} else {quote_next_list || quoted}));
|
||||
i = close_paren_idx;
|
||||
quote_next_list = false;
|
||||
unquote_next_list = false;
|
||||
},
|
||||
Token::Identifier(name) => {
|
||||
values.push(PreLispValue::Var(name.to_owned()));
|
||||
},
|
||||
Token::Integer(num) => {
|
||||
values.push(PreLispValue::Integer(*num));
|
||||
},
|
||||
Token::Float(num) => {
|
||||
values.push(PreLispValue::Float(*num));
|
||||
},
|
||||
Token::String(str) => {
|
||||
values.push(PreLispValue::String(str.to_owned()));
|
||||
},
|
||||
Token::CloseParen => {
|
||||
panic!("Unexpected closing parenthesis");
|
||||
},
|
||||
|
||||
i += 1;
|
||||
}
|
||||
i += 1;
|
||||
|
||||
opcodes.push(OpCode::Call(name.to_owned(), args))
|
||||
}
|
||||
|
||||
PreLispValue::List(values, quoted)
|
||||
opcodes
|
||||
}
|
||||
|
||||
pub fn parse(tokens: Vec<Token>) -> Vec<PreLispValue> {
|
||||
let mut values = Vec::new();
|
||||
pub fn parse(tokens: Vec<Token>) -> Vec<OpCode> {
|
||||
let mut opcodes = Vec::new();
|
||||
|
||||
let mut i = 0;
|
||||
while i < tokens.len() {
|
||||
match &tokens[i] {
|
||||
Token::OpenParen => {
|
||||
let (tkns, close_paren_idx) = read_exp(i, &tokens).unwrap();
|
||||
values.push(parse_exp(tkns, false));
|
||||
opcodes.push(OpCode::Exp(parse_exp(tkns)));
|
||||
i = close_paren_idx;
|
||||
}
|
||||
tkn => {
|
||||
|
@ -150,5 +147,5 @@ pub fn parse(tokens: Vec<Token>) -> Vec<PreLispValue> {
|
|||
i += 1;
|
||||
}
|
||||
|
||||
values
|
||||
opcodes
|
||||
}
|
|
@ -11,4 +11,4 @@
|
|||
; LispValue::Float(1.0),
|
||||
; LispValue::Integer(3)
|
||||
; })
|
||||
; })
|
||||
; })
|
||||
|
|
|
@ -4,8 +4,6 @@ use std::fmt;
|
|||
pub enum Token {
|
||||
OpenParen,
|
||||
CloseParen,
|
||||
Quote,
|
||||
Unquote,
|
||||
Identifier(String),
|
||||
String(String),
|
||||
Integer(i32),
|
||||
|
@ -115,8 +113,6 @@ impl Tokenizer {
|
|||
tokens.push(Token::Integer(self.storage.iter().collect::<String>().parse().unwrap()));
|
||||
}
|
||||
|
||||
self.is_float = false;
|
||||
|
||||
self.storage.clear();
|
||||
} else {
|
||||
if char == '.' {
|
||||
|
@ -168,8 +164,6 @@ impl Tokenizer {
|
|||
self.reading_string = true;
|
||||
self.storage.clear();
|
||||
},
|
||||
'\'' => tokens.push(Token::Quote),
|
||||
',' => tokens.push(Token::Unquote),
|
||||
c => {
|
||||
if c.is_alphabetic() {
|
||||
self.reading_identifier = true;
|
||||
|
|
Reference in a new issue