Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
feat: add cli reading
Browse files Browse the repository at this point in the history
  • Loading branch information
aripiprazole committed Dec 30, 2023
1 parent ef5671d commit b817a8d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
15 changes: 12 additions & 3 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ macro_rules! bail {

/// A value in the language. It's the lowest level of representation of a
/// value, and is used for both the AST and the runtime.
#[derive(Clone)]
#[derive(Clone, Default)]
pub enum Value {
Int(u64),
Keyword(Keyword),
Expand All @@ -60,9 +60,18 @@ pub enum Value {
Recur(Vec<Value>),
Quote(Expr),
Ptr(*mut ()),

#[default]
Nil,
}

impl Value {
/// Reads the values into S-Expressions again
pub fn readback(self) -> Term {
todo!()
}
}

#[derive(Debug, Hash, Clone, PartialEq, Eq)]
pub struct Keyword {
pub text: String,
Expand All @@ -76,7 +85,7 @@ impl Keyword {
}

/// The environment in which evaluation takes place.
#[derive(Clone)]
#[derive(Clone, Default)]
pub struct Environment {
pub global: Value,
pub expanded: bool,
Expand Down Expand Up @@ -260,7 +269,7 @@ fn apply_expand(apply: crate::Apply, environment: &Environment) -> Result<Value,

fun.call(environment, arguments).eval_into_result()
}
None | Some(_) => Ok(Value::Apply {
_ => Ok(Value::Apply {
callee: Value::Keyword(Keyword::from(k.clone())).into(),
arguments: apply
.spine()?
Expand Down
12 changes: 11 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use rustyline::{
error::ReadlineError, validate::MatchingBracketValidator, Completer, Editor, Helper,
Highlighter, Hinter, Validator,
};
use soft::{eval, Expr, Term};

/// Simple program to greet a person
#[derive(Parser, Debug)]
Expand Down Expand Up @@ -48,6 +49,7 @@ fn get_history_path() -> Option<PathBuf> {

pub fn repl() {
let mut rl = Editor::new().expect("cannot create repl");
let environment = eval::Environment::default();
let path = get_history_path();
let h = InputValidator {
brackets: MatchingBracketValidator::new(),
Expand All @@ -65,7 +67,15 @@ pub fn repl() {
match rl.readline("> ") {
Ok(line) => {
rl.add_history_entry(line.as_str()).unwrap();
println!("Line: {}", line)
let value = soft::parser::parse_sexpr(&line)
.and_then(|sexpr| Expr::try_from(sexpr).map_err(|error| error.into()))
.and_then(|expr| expr.expand(&environment))
.and_then(|expr| expr.eval(&environment).eval_into_result());

match value {
Ok(value) => println!("< {}", value.readback()),
Err(error) => eprintln!("- {}", Term::from(error)),
}
}
Err(ReadlineError::Interrupted) => {
println!("Interrupted");
Expand Down
12 changes: 6 additions & 6 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{iter::Peekable, str::Chars};

use crate::{SrcPos, Term};
use crate::{SrcPos, Term, Expr, keyword};

pub fn is_identifier_char(c: char) -> bool {
c != ' ' && c != '\n' && c != '\t' && c != '(' && c != ')' && c != '"' && c != ';'
Expand Down Expand Up @@ -36,7 +36,7 @@ impl<'a> Parser<'a> {
string
}

pub fn parse(&mut self) -> Result<Term, String> {
pub fn parse(&mut self) -> Result<Term, Expr> {
let start = self.index;

let result = match self.peek() {
Expand All @@ -53,7 +53,7 @@ impl<'a> Parser<'a> {
let string = self.accumulate(|c| c != '"');

if self.bump() != Some('"') {
return Err("expected '\"'".to_string());
return Err(keyword!("parser.error/unexpected-quote"));
}

Ok(Term::String(string))
Expand Down Expand Up @@ -83,7 +83,7 @@ impl<'a> Parser<'a> {
Some(_) => {
terms.push(self.parse()?);
}
None => return Err("unexpected end of file".to_string()),
None => return Err(keyword!("parser.error/unexpected-end-of-file")),
}
}

Expand All @@ -93,7 +93,7 @@ impl<'a> Parser<'a> {
let string = self.accumulate(is_identifier_char);
Ok(Term::Identifier(string))
}
None => Err("unexpected end of file".to_string()),
None => Err(keyword!("parser.error/unexpected-end-of-file")),
}?;

Ok(Term::SrcPos(
Expand All @@ -106,7 +106,7 @@ impl<'a> Parser<'a> {
}
}

pub fn parse_sexpr(string: &str) -> Result<Term, String> {
pub fn parse_sexpr(string: &str) -> Result<Term, Expr> {
let mut parser = Parser {
peekable: string.chars().peekable(),
string,
Expand Down

0 comments on commit b817a8d

Please sign in to comment.