This is a rust project now, let's end the segregation
This commit is contained in:
20
src/Makefile
20
src/Makefile
@@ -1,20 +0,0 @@
|
||||
.PHONY: all clean
|
||||
all: deelang
|
||||
CC = g++
|
||||
CPPFLAGS = -g
|
||||
|
||||
lexer.o lexer.h: parser.h lexer.l
|
||||
flex -o lexer.cpp --header-file=lexer.h lexer.l
|
||||
$(CC) -c lexer.cpp
|
||||
rm lexer.cpp
|
||||
|
||||
parser.h parser.o: parser.y
|
||||
bison -o parser.cpp --header=parser.h parser.y
|
||||
$(CC) -c parser.cpp
|
||||
rm parser.cpp
|
||||
|
||||
deelang: deelang.cpp lexer.o parser.o syntax.o
|
||||
$(CC) $(CPPFLAGS) -o $@ $^
|
||||
|
||||
clean:
|
||||
rm -rf *.o parser.h lexer.h deelang
|
||||
@@ -1,11 +0,0 @@
|
||||
#include "syntax.h"
|
||||
#include "lexer.h"
|
||||
#include "parser.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
yyparse();
|
||||
|
||||
return 0;
|
||||
}
|
||||
72
src/emitter.rs
Normal file
72
src/emitter.rs
Normal file
@@ -0,0 +1,72 @@
|
||||
use std::io::Write;
|
||||
use std::collections::HashSet;
|
||||
|
||||
use crate::parser;
|
||||
|
||||
pub struct LexicalContext<'a> {
|
||||
parent: Option<&'a LexicalContext<'a>>,
|
||||
local: HashSet<String>
|
||||
}
|
||||
|
||||
impl <'a> LexicalContext<'a> {
|
||||
pub fn toplevel() -> Self {
|
||||
LexicalContext {
|
||||
parent: None,
|
||||
local: HashSet::new()
|
||||
}
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
fn new(parent: &'a LexicalContext<'a>) -> Self {
|
||||
LexicalContext {
|
||||
parent: Some(parent),
|
||||
local: HashSet::new(),
|
||||
}
|
||||
}
|
||||
fn contains(&self, s: &str) -> bool {
|
||||
self.local.contains(s) || self.parent.map_or(false, |c| c.contains(s))
|
||||
}
|
||||
fn insert(&mut self, s: String) -> bool {
|
||||
self.local.insert(s)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn emit_all(w: &mut dyn Write, ast: &Vec<parser::Stmt>, ctx: &mut LexicalContext) -> std::io::Result<()> {
|
||||
for stmt in ast {
|
||||
emit(w, stmt, ctx)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn emit(w: &mut dyn Write, stmt: &parser::Stmt, ctx: &mut LexicalContext) -> std::io::Result<()> {
|
||||
match &stmt {
|
||||
parser::Stmt::ReplPrint(expr) => {
|
||||
write!(w, "console.log((")?;
|
||||
emit_expr(w, expr, ctx)?;
|
||||
writeln!(w, "));")?;
|
||||
}
|
||||
parser::Stmt::Assignment(id, expr) => {
|
||||
if !ctx.contains(id) {
|
||||
ctx.insert(id.clone());
|
||||
write!(w, "let ")?;
|
||||
}
|
||||
write!(w, "{} = ", id)?;
|
||||
emit_expr(w, expr, ctx)?;
|
||||
writeln!(w, ";")?;
|
||||
}
|
||||
_ => todo!(),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn emit_expr(w: &mut dyn Write, expr: &parser::Expr, ctx: &mut LexicalContext) -> std::io::Result<()> {
|
||||
match &expr {
|
||||
parser::Expr::Id(id) => {
|
||||
write!(w, "{}", id)?;
|
||||
}
|
||||
parser::Expr::Atom(atom) => {
|
||||
write!(w, "{}", atom)?;
|
||||
}
|
||||
_ => todo!(),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
137
src/evaluator.rs
Normal file
137
src/evaluator.rs
Normal file
@@ -0,0 +1,137 @@
|
||||
use std::ops;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::parser::{self, GuardedBlock};
|
||||
use parser::Atom;
|
||||
|
||||
pub struct Env<'a> {
|
||||
pub parent: Option<&'a Env<'a>>,
|
||||
pub values: HashMap<String, Atom>
|
||||
}
|
||||
|
||||
impl<'a> Env<'a> {
|
||||
pub fn global() -> Self {
|
||||
Env {
|
||||
parent: None,
|
||||
values: HashMap::new(),
|
||||
}
|
||||
}
|
||||
pub fn child(parent: &'a Env<'a>) -> Self {
|
||||
Env {
|
||||
parent: Some(parent),
|
||||
values: HashMap::new(),
|
||||
}
|
||||
}
|
||||
pub fn lookup(&self, id: &str) -> Atom {
|
||||
if let Some(a) = self.values.get(id) {
|
||||
a.clone()
|
||||
} else if let Some(parent) = self.parent {
|
||||
parent.lookup(id)
|
||||
} else {
|
||||
panic!("Variable {} not in scope.", id)
|
||||
}
|
||||
}
|
||||
pub fn set(&mut self, id: String, atom: Atom) {
|
||||
self.values.insert(id, atom);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eval(ast: &parser::Stmt, env: &mut Env) {
|
||||
match &ast {
|
||||
parser::Stmt::ReplPrint(expr) =>
|
||||
println!("{}", eval_expr(expr, env)),
|
||||
parser::Stmt::Assignment(id, expr) =>
|
||||
env.set(id.clone(), eval_expr(expr, env)),
|
||||
parser::Stmt::Conditional(guarded_blocks, default_block) => {
|
||||
let mut matched = false;
|
||||
for GuardedBlock { guard, block } in guarded_blocks {
|
||||
let res = eval_expr(guard, env);
|
||||
match res {
|
||||
Atom::Bool(true) => {
|
||||
matched = true;
|
||||
for stmt in block {
|
||||
eval(stmt, env);
|
||||
}
|
||||
break;
|
||||
}
|
||||
Atom::Bool(false) => continue,
|
||||
_ => panic!("Conditional expression does not evaluate to a bool"),
|
||||
}
|
||||
}
|
||||
if !matched {
|
||||
if let Some(block) = default_block {
|
||||
eval_expr(block, env);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eval_expr(ast: &parser::Expr, env: &Env) -> Atom {
|
||||
match ast {
|
||||
parser::Expr::Id(a) => env.lookup(a),
|
||||
parser::Expr::Atom(a) => a.clone(),
|
||||
parser::Expr::UnaryMinus(a) => -eval_expr(a, env),
|
||||
parser::Expr::Plus(a, b) => eval_expr(a, env) + eval_expr(b, env),
|
||||
parser::Expr::Minus(a, b) => eval_expr(a, env) - eval_expr(b, env),
|
||||
parser::Expr::Mult(a, b) => eval_expr(a, env) * eval_expr(b, env),
|
||||
parser::Expr::Div(a, b) => eval_expr(a, env) / eval_expr(b, env),
|
||||
_ => panic!("Couldn't evalute expression {{ {:?} }}", ast),
|
||||
}
|
||||
}
|
||||
|
||||
impl ops::Neg for Atom {
|
||||
type Output = Self;
|
||||
|
||||
fn neg(self) -> Self {
|
||||
match self {
|
||||
Atom::Num(a) => Atom::Num(-a),
|
||||
_ => panic!("Can't negate non-numeral type!"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ops::Add for Atom {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, other: Self) -> Self {
|
||||
match (self, other) {
|
||||
(Atom::Num(a), Atom::Num(b)) => Atom::Num(a + b),
|
||||
_ => panic!("Can't add non-numeral types!"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ops::Sub for Atom {
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, other: Self) -> Self {
|
||||
match (self, other) {
|
||||
(Atom::Num(a), Atom::Num(b)) => Atom::Num(a - b),
|
||||
_ => panic!("Can't subtract non-numeral types!"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ops::Mul for Atom {
|
||||
type Output = Self;
|
||||
|
||||
fn mul(self, other: Self) -> Self {
|
||||
match (self, other) {
|
||||
(Atom::Num(a), Atom::Num(b)) => Atom::Num(a * b),
|
||||
_ => panic!("Can't multiply non-numeral types!"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ops::Div for Atom {
|
||||
type Output = Self;
|
||||
|
||||
fn div(self, other: Self) -> Self {
|
||||
match (self, other) {
|
||||
(Atom::Num(a), Atom::Num(b)) => Atom::Num(a / b),
|
||||
_ => panic!("Can't divide non-numeral types!"),
|
||||
}
|
||||
}
|
||||
}
|
||||
54
src/lexer.l
54
src/lexer.l
@@ -1,54 +0,0 @@
|
||||
%{
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include "syntax.h"
|
||||
#include "parser.h"
|
||||
using namespace std;
|
||||
|
||||
stack<int> s;
|
||||
|
||||
#define YY_USER_INIT s.push(0); BEGIN(freshline);
|
||||
|
||||
%}
|
||||
|
||||
%option noyywrap
|
||||
|
||||
%x freshline
|
||||
|
||||
digit [0-9]
|
||||
letter [A-Za-z]
|
||||
|
||||
%%
|
||||
<freshline>""/[^\t ] {
|
||||
if (s.top() == 0) {
|
||||
BEGIN(0);
|
||||
} else {
|
||||
s.pop(); yyless(0); return DEDENT;
|
||||
}
|
||||
}
|
||||
<freshline>[ \t]+ {
|
||||
if (s.top() == yyleng) {
|
||||
BEGIN(0); // Same indentation, continue
|
||||
} else if (s.top() > yyleng) {
|
||||
s.pop(); yyless(0); return DEDENT;
|
||||
// Same rule again until the stack is even
|
||||
} else {
|
||||
s.push(yyleng); BEGIN(0); return INDENT;
|
||||
}
|
||||
}
|
||||
#[^\n]* // Eat comments
|
||||
\"[^"]*\" yylval.sym = new string(yytext); return STRING;
|
||||
if return IF;
|
||||
else return ELSE;
|
||||
elif return ELIF;
|
||||
{digit}+|{digit}*\.{digit}+ yylval.num = atof(yytext); return NUM;
|
||||
{letter}({letter}|{digit}|[?.-])* yylval.sym = new string(yytext); return ID;
|
||||
"<-" return GETS;
|
||||
"->" return MAPS;
|
||||
".." return CAT;
|
||||
[(){}.,*/+-] return yytext[0];
|
||||
[\t ] // Eat whitespace not first on a line
|
||||
"/"\n // Eat newlines ending in /
|
||||
[\n;] BEGIN(freshline); return STOP;
|
||||
. fprintf(stderr, "Scanning error!\nOffender: %s\n", yytext); exit(1);
|
||||
%%
|
||||
63
src/main.rs
Normal file
63
src/main.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
mod parser;
|
||||
mod evaluator;
|
||||
mod emitter;
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
use std::io;
|
||||
use std::io::prelude::*;
|
||||
use std::fs::File;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(author, version, about = "Interpreter for Deelang")]
|
||||
struct Cli {
|
||||
#[clap(help="Specify a file to run")]
|
||||
file: Option<PathBuf>,
|
||||
#[clap(short, long, help="Only parse, do not evaluate")]
|
||||
parse_only: bool,
|
||||
#[clap(short, long, help="Cross-compile to ECMAScript")]
|
||||
ecmascript: bool,
|
||||
}
|
||||
|
||||
fn repl(cli: &Cli) {
|
||||
let mut global = evaluator::Env::global();
|
||||
let mut toplevel = emitter::LexicalContext::toplevel();
|
||||
let mut out = io::stdout();
|
||||
loop {
|
||||
let mut line = String::new();
|
||||
io::stdin().read_line(&mut line).unwrap();
|
||||
let tree = parser::parse_stmt(&line);
|
||||
if cli.parse_only {
|
||||
println!("{:#?}", tree);
|
||||
} else if cli.ecmascript {
|
||||
emitter::emit(&mut out, &tree, &mut toplevel).ok();
|
||||
} else {
|
||||
evaluator::eval(&tree, &mut global);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn script(cli: &Cli) {
|
||||
let mut file = File::open(cli.file.as_ref().unwrap()).expect("Could not read file");
|
||||
let mut prgm = String::new();
|
||||
file.read_to_string(&mut prgm).unwrap();
|
||||
let tree = parser::parse(&prgm);
|
||||
if cli.parse_only {
|
||||
println!("{:#?}", tree);
|
||||
} else if cli.ecmascript {
|
||||
let mut out = io::stdout();
|
||||
let mut toplevel = emitter::LexicalContext::toplevel();
|
||||
emitter::emit_all(&mut out, &tree, &mut toplevel).ok();
|
||||
} else {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cli = Cli::parse();
|
||||
match cli.file {
|
||||
None => repl(&cli),
|
||||
Some(_) => script(&cli),
|
||||
}
|
||||
}
|
||||
386
src/parser.rs
Normal file
386
src/parser.rs
Normal file
@@ -0,0 +1,386 @@
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug,PartialEq,Clone)]
|
||||
pub enum Stmt {
|
||||
Assignment(String, Expr),
|
||||
Funcall(Expr),
|
||||
Conditional(Vec<GuardedBlock>, Option<Expr>),
|
||||
ReplPrint(Expr),
|
||||
}
|
||||
|
||||
#[derive(Debug,PartialEq,Clone)]
|
||||
pub enum Expr {
|
||||
Id(String),
|
||||
Atom(Atom),
|
||||
Funcall(String, Vec<Expr>),
|
||||
Funcdef(Option<String>, Box<Expr>),
|
||||
UnaryMinus(Box<Expr>),
|
||||
Plus(Box<Expr>, Box<Expr>),
|
||||
Minus(Box<Expr>, Box<Expr>),
|
||||
Mult(Box<Expr>, Box<Expr>),
|
||||
Div(Box<Expr>, Box<Expr>),
|
||||
Block(Vec<Stmt>),
|
||||
Object(Vec<Stmt>),
|
||||
}
|
||||
|
||||
#[derive(Debug,PartialEq,Clone)]
|
||||
pub enum Atom {
|
||||
String(String),
|
||||
Num(f64),
|
||||
Bool(bool),
|
||||
}
|
||||
|
||||
impl fmt::Display for Atom {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Atom::String(a) => write!(f, "\"{}\"", a),
|
||||
Atom::Num(a) => write!(f, "{}", a),
|
||||
Atom::Bool(a) => write!(f, "{}", a),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug,PartialEq,Clone)]
|
||||
pub struct GuardedBlock {
|
||||
pub guard: Expr,
|
||||
pub block: Vec<Stmt>,
|
||||
}
|
||||
|
||||
peg::parser! {
|
||||
grammar deelang_parser() for str {
|
||||
pub rule program() -> Vec<Stmt>
|
||||
= __* s:stmt()* { s }
|
||||
pub rule stmt() -> Stmt
|
||||
= a:assignment() { a } /
|
||||
f:funcall() stop() { Stmt::Funcall(f) } /
|
||||
c:conditional() { c } /
|
||||
e:expr() stop() { Stmt::ReplPrint(e) }
|
||||
rule expr() -> Expr = precedence! {
|
||||
"-" _ e1:@ { Expr::UnaryMinus(Box::new(e1)) }
|
||||
--
|
||||
e1:(@) "+" _ e2:@ { Expr::Plus(Box::new(e1), Box::new(e2)) }
|
||||
e1:(@) "-" _ e2:@ { Expr::Minus(Box::new(e1), Box::new(e2)) }
|
||||
--
|
||||
e1:(@) "*" _ e2:@ { Expr::Mult(Box::new(e1), Box::new(e2)) }
|
||||
e1:(@) "/" _ e2:@ { Expr::Div(Box::new(e1), Box::new(e2)) }
|
||||
--
|
||||
"(" _ e:expr() ")" _ { e }
|
||||
['"'] s:$((!['"'] [_] / r#"\""#)*) ['"'] { Expr::Atom(Atom::String(s.to_string())) }
|
||||
f:funcall() { f }
|
||||
f:funcdef() { f }
|
||||
b:boolean() _ { Expr::Atom(Atom::Bool(b)) }
|
||||
o:object() { o }
|
||||
i:id() _ { Expr::Id(i) }
|
||||
n:num() _ { Expr::Atom(Atom::Num(n)) }
|
||||
|
||||
}
|
||||
|
||||
rule boolean() -> bool
|
||||
= b:$("true" / "false") { b.parse().unwrap() }
|
||||
rule id() -> String
|
||||
= i:$(letter() (letter() / digit() / ['?'|'.'|'-'])*) _ { i.to_string() }
|
||||
rule assignment() -> Stmt
|
||||
= i:id() "<-" _ e:expr() stop() { Stmt::Assignment(i, e) }
|
||||
rule num() -> f64
|
||||
= n:$(digit()+ "."? digit()* / "." digit()+) _ { n.parse().unwrap() }
|
||||
rule funcall() -> Expr
|
||||
= i:id() "(" _ e:(expr() ** ("," _)) ")" _ { Expr::Funcall(i, e) }
|
||||
rule funcdef() -> Expr
|
||||
= i:id()? "->" _ e:(expr() / block()) { Expr::Funcdef(i, Box::new(e)) }
|
||||
rule conditional() -> Stmt
|
||||
= i:_if() __* ei:elif()* __* e:_else()? __* { Stmt::Conditional([vec![i], ei].concat(), e) }
|
||||
rule object() -> Expr
|
||||
= "{" _ stop() indent() __* a:assignment()+ dedent() __* "}" _ { Expr::Object(a) }
|
||||
|
||||
rule _if() -> GuardedBlock
|
||||
= "if" _ g:expr() b:indented_block() {
|
||||
GuardedBlock {
|
||||
guard: g,
|
||||
block: b,
|
||||
}
|
||||
}
|
||||
rule elif() -> GuardedBlock
|
||||
= "elif" _ g:expr() b:indented_block() {
|
||||
GuardedBlock {
|
||||
guard: g,
|
||||
block: b
|
||||
}
|
||||
}
|
||||
rule _else() -> Expr
|
||||
= "else" _ b:block() { b }
|
||||
rule block() -> Expr
|
||||
= i:indented_block() { Expr::Block(i) }
|
||||
rule indented_block() -> Vec<Stmt>
|
||||
= stop() indent() __* s:stmt()+ dedent() { s }
|
||||
|
||||
rule letter()
|
||||
= ['A'..='Z'] / ['a'..='z']
|
||||
rule digit()
|
||||
= ['0'..='9']
|
||||
rule stop()
|
||||
= __+ / eof()
|
||||
rule indent()
|
||||
= ">>>"
|
||||
rule dedent()
|
||||
= "<<<"
|
||||
rule _ // Non-meaningful whitespace
|
||||
= ['\t'|' ']*
|
||||
rule __ // End Of Statement (comment, newline, eof, TODO semicolon)
|
||||
= comment()? newline() / comment() &eof()
|
||||
rule comment()
|
||||
= "#" (!newline() [_])* &(newline() / eof())
|
||||
rule newline()
|
||||
= "\r\n" / "\r" / "\n"
|
||||
rule eof()
|
||||
= ![_]
|
||||
}
|
||||
}
|
||||
|
||||
fn preprocess(input: &str) -> String {
|
||||
let mut stack = vec![0];
|
||||
let mut output = String::new();
|
||||
for line in input.lines() {
|
||||
let mut count = 0;
|
||||
for c in line.chars() {
|
||||
if c == ' ' || c == '\t' {
|
||||
count += 1;
|
||||
} else if c == '#' {
|
||||
break;
|
||||
} else {
|
||||
use std::cmp::Ordering::*;
|
||||
let curr = stack.last().unwrap();
|
||||
match curr.cmp(&count) {
|
||||
Less => {
|
||||
stack.push(count);
|
||||
output.push_str(">>>");
|
||||
},
|
||||
Greater => {
|
||||
while stack.last().unwrap() > &count {
|
||||
output.push_str("<<<");
|
||||
output.push('\n');
|
||||
stack.pop();
|
||||
}
|
||||
}
|
||||
Equal => (),
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
output.push_str(line.trim());
|
||||
output.push('\n');
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
pub fn parse(prgm: &str) -> Vec<Stmt> {
|
||||
let prgm = preprocess(prgm);
|
||||
deelang_parser::program(&prgm).unwrap()
|
||||
}
|
||||
|
||||
pub fn parse_stmt(stmt: &str) -> Stmt {
|
||||
let stmt = preprocess(stmt);
|
||||
deelang_parser::stmt(&stmt).unwrap()
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_comments() {
|
||||
let prgm = r"## This is a comment
|
||||
apple <- 1 ## This is too
|
||||
## This comment ends the file";
|
||||
let expected = vec![Stmt::Assignment("apple".to_string(), Expr::Atom(Atom::Num(1.0)))];
|
||||
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_funcall() {
|
||||
let expected = vec![
|
||||
Stmt::Funcall(Expr::Funcall("pear".to_string(), vec![])),
|
||||
Stmt::Funcall(Expr::Funcall("pear".to_string(),
|
||||
vec![Expr::Id("x".to_string()), Expr::Id("y".to_string())],
|
||||
))
|
||||
];
|
||||
let prgm = r"pear()
|
||||
pear(x, y)";
|
||||
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_assignment() {
|
||||
let prgm = r"apple <- 1
|
||||
apple <- pear(x, y)";
|
||||
let expected = vec![
|
||||
Stmt::Assignment("apple".to_string(),
|
||||
Expr::Atom(Atom::Num(1.0))),
|
||||
Stmt::Assignment("apple".to_string(),
|
||||
Expr::Funcall("pear".to_string(),
|
||||
vec![
|
||||
Expr::Id("x".to_string()),
|
||||
Expr::Id("y".to_string()),
|
||||
])),
|
||||
];
|
||||
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_operators() {
|
||||
let prgm = r"three <- 1 + 2
|
||||
one <- 3 - 2
|
||||
four <- (3 - 1) * 2";
|
||||
let expected = vec![
|
||||
Stmt::Assignment("three".to_string(),
|
||||
Expr::Plus(
|
||||
Box::new(Expr::Atom(Atom::Num(1.0))),
|
||||
Box::new(Expr::Atom(Atom::Num(2.0))),
|
||||
)),
|
||||
Stmt::Assignment("one".to_string(),
|
||||
Expr::Minus(
|
||||
Box::new(Expr::Atom(Atom::Num(3.0))),
|
||||
Box::new(Expr::Atom(Atom::Num(2.0))),
|
||||
)),
|
||||
Stmt::Assignment("four".to_string(),
|
||||
Expr::Mult(
|
||||
Box::new(Expr::Minus(
|
||||
Box::new(Expr::Atom(Atom::Num(3.0))),
|
||||
Box::new(Expr::Atom(Atom::Num(1.0))),
|
||||
)),
|
||||
Box::new(Expr::Atom(Atom::Num (2.0))),
|
||||
))
|
||||
];
|
||||
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_compound_expression() {
|
||||
let prgm = "apple <- pear(x, y) + z";
|
||||
let expected = vec![
|
||||
Stmt::Assignment("apple".to_string(),
|
||||
Expr::Plus(
|
||||
Box::new(Expr::Funcall(
|
||||
"pear".to_string(),
|
||||
vec![Expr::Id("x".to_string()), Expr::Id("y".to_string())],
|
||||
)),
|
||||
Box::new(Expr::Id("z".to_string()))
|
||||
)
|
||||
),
|
||||
];
|
||||
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_funcdef() {
|
||||
let prgm = r"foo <- -> bar()
|
||||
foo <- ->
|
||||
>>>
|
||||
bar()
|
||||
baz()
|
||||
<<<
|
||||
foo <- x -> y -> x * y";
|
||||
let expected = vec![
|
||||
Stmt::Assignment(
|
||||
"foo".to_string(),
|
||||
Expr::Funcdef(
|
||||
None,
|
||||
Box::new(Expr::Funcall("bar".to_string(), vec![])),
|
||||
)
|
||||
),
|
||||
Stmt::Assignment(
|
||||
"foo".to_string(),
|
||||
Expr::Funcdef(
|
||||
None,
|
||||
Box::new(Expr::Block(vec![
|
||||
Stmt::Funcall(Expr::Funcall("bar".to_string(), vec![])),
|
||||
Stmt::Funcall(Expr::Funcall("baz".to_string(), vec![])),
|
||||
]))
|
||||
)
|
||||
),
|
||||
Stmt::Assignment(
|
||||
"foo".to_string(),
|
||||
Expr::Funcdef(
|
||||
Some("x".to_string()),
|
||||
Box::new(
|
||||
Expr::Funcdef(
|
||||
Some("y".to_string()),
|
||||
Box::new(
|
||||
Expr::Mult(
|
||||
Box::new(Expr::Id("x".to_string())),
|
||||
Box::new(Expr::Id("y".to_string())),
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
];
|
||||
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
|
||||
}
|
||||
#[test]
|
||||
fn test_conditional() {
|
||||
let prgm = r"if foo
|
||||
>>>bar()
|
||||
<<<
|
||||
elif baz
|
||||
>>>foobar()
|
||||
<<<
|
||||
else
|
||||
>>>quux()
|
||||
<<<";
|
||||
let expected = vec![Stmt::Conditional(
|
||||
vec![
|
||||
GuardedBlock {
|
||||
guard: Expr::Id("foo".to_string()),
|
||||
block: vec![Stmt::Funcall(Expr::Funcall("bar".to_string(), vec![]))]
|
||||
},
|
||||
GuardedBlock {
|
||||
guard: Expr::Id("baz".to_string()),
|
||||
block: vec![Stmt::Funcall(Expr::Funcall("foobar".to_string(), vec![]))]
|
||||
},
|
||||
],
|
||||
Some(Expr::Block(vec![Stmt::Funcall(Expr::Funcall("quux".to_string(), vec![]))])),
|
||||
)];
|
||||
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
|
||||
}
|
||||
#[test]
|
||||
fn test_object() {
|
||||
let prgm = r"fruit <- {
|
||||
>>>apple <- 1
|
||||
pear <- 2
|
||||
<<<
|
||||
}";
|
||||
let expected = vec![Stmt::Assignment(
|
||||
"fruit".to_string(),
|
||||
Expr::Object(vec![
|
||||
Stmt::Assignment("apple".to_string(), Expr::Atom(Atom::Num(1.0))),
|
||||
Stmt::Assignment("pear".to_string(), Expr::Atom(Atom::Num(2.0))),
|
||||
]),
|
||||
)];
|
||||
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_preprocess() {
|
||||
let prgm = r"
|
||||
.
|
||||
.
|
||||
.
|
||||
.
|
||||
.
|
||||
## Hello World";
|
||||
let expected = r"
|
||||
>>>.
|
||||
>>>.
|
||||
<<<
|
||||
.
|
||||
>>>.
|
||||
<<<
|
||||
<<<
|
||||
.
|
||||
## Hello World
|
||||
";
|
||||
assert_eq!(preprocess(prgm), expected);
|
||||
}
|
||||
}
|
||||
78
src/parser.y
78
src/parser.y
@@ -1,78 +0,0 @@
|
||||
%{
|
||||
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
#include "syntax.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int yylex();
|
||||
void yyerror(const char* p) { fprintf(stderr, p); }
|
||||
|
||||
%}
|
||||
|
||||
%define parse.trace
|
||||
|
||||
%union {
|
||||
float num;
|
||||
std::string *sym;
|
||||
Node *expr;
|
||||
NodeList *exprlist;
|
||||
}
|
||||
|
||||
%token <sym> ID
|
||||
%token <num> NUM
|
||||
%token <sym> STRING
|
||||
|
||||
%token STOP
|
||||
%token INDENT DEDENT
|
||||
%token IF ELIF ELSE
|
||||
|
||||
%right GETS
|
||||
%left CAT
|
||||
%left '+' '-'
|
||||
%left '/' '*'
|
||||
%left MAPS
|
||||
|
||||
%nterm <expr> expr
|
||||
%nterm <exprlist> exprlist
|
||||
|
||||
%%
|
||||
program: statements | statements statement;
|
||||
statements: statements statement STOP
|
||||
| statements STOP
|
||||
| // null production ;
|
||||
statement: assignment | funcall | conditional;
|
||||
assignment: ID GETS expr;
|
||||
assignments: assignments assignment STOP | // null production;
|
||||
expr: funcdef
|
||||
| funcall
|
||||
| objdef
|
||||
| expr CAT expr { $$ = new OpNode(CAT, $1, $3); }
|
||||
| expr '+' expr { $$ = new OpNode('+', $1, $3); }
|
||||
| expr '-' expr { $$ = new OpNode('-', $1, $3); }
|
||||
| expr '*' expr { $$ = new OpNode('*', $1, $3); }
|
||||
| expr '/' expr { $$ = new OpNode('/', $1, $3); }
|
||||
| '(' expr ')' {$$ = $2;}
|
||||
| ID {$$ = new IdNode($1);}
|
||||
| NUM {$$ = new NumNode($1);}
|
||||
| STRING {$$ = new StringNode($1);};
|
||||
|
||||
funcdef: param MAPS expr | param MAPS block;
|
||||
param: ID | '_' | // null production;
|
||||
|
||||
funcall: ID '(' exprlist ')' { print_expression_list($3); }
|
||||
exprlist: exprlist ',' expr { $1 -> push_back($3); $$ = $1; }
|
||||
| expr { $$ = new NodeList(); $$ -> push_back($1); }
|
||||
| { $$ = new NodeList(); } // null production;
|
||||
|
||||
conditional: IF expr block elifs
|
||||
| IF expr block elifs ELSE block;
|
||||
elifs: ELIF expr block elifs| // null production;
|
||||
|
||||
objdef: '{' block_assignments '}' | '{' '}'
|
||||
|
||||
block: STOP INDENT statement statements DEDENT
|
||||
block_assignments: STOP INDENT assignments DEDENT
|
||||
%%
|
||||
@@ -1,49 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "syntax.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
IdNode::IdNode(string *id) {
|
||||
this->id = id;
|
||||
}
|
||||
|
||||
void IdNode::display() {
|
||||
cout << *id;
|
||||
}
|
||||
|
||||
NumNode::NumNode(float num) {
|
||||
this->num = num;
|
||||
}
|
||||
|
||||
void NumNode::display() {
|
||||
cout << num;
|
||||
}
|
||||
|
||||
StringNode::StringNode(string* stringVal) {
|
||||
this->stringVal = stringVal;
|
||||
}
|
||||
|
||||
void StringNode::display() {
|
||||
cout << *stringVal;
|
||||
}
|
||||
|
||||
OpNode::OpNode(int op, Node *first, Node *second) {
|
||||
this->op = op;
|
||||
this->first = first;
|
||||
this->second = second;
|
||||
}
|
||||
|
||||
void OpNode::display() {
|
||||
first->display();
|
||||
cout << " " << (char)op << " ";
|
||||
second->display();
|
||||
}
|
||||
|
||||
void print_expression_list(NodeList *list) {
|
||||
for (Node *expr : *list) {
|
||||
expr->display();
|
||||
}
|
||||
}
|
||||
50
src/syntax.h
50
src/syntax.h
@@ -1,50 +0,0 @@
|
||||
#ifndef SYNTAX_H
|
||||
#define SYNTAX_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
class Node {
|
||||
public:
|
||||
virtual void display() = 0;
|
||||
};
|
||||
|
||||
class IdNode : public Node {
|
||||
private:
|
||||
std::string *id;
|
||||
public:
|
||||
IdNode(std::string*);
|
||||
virtual void display();
|
||||
};
|
||||
|
||||
class NumNode : public Node {
|
||||
private:
|
||||
float num;
|
||||
public:
|
||||
NumNode(float);
|
||||
virtual void display();
|
||||
};
|
||||
|
||||
class StringNode : public Node {
|
||||
private:
|
||||
std::string* stringVal;
|
||||
public:
|
||||
StringNode(std::string*);
|
||||
virtual void display();
|
||||
};
|
||||
|
||||
class OpNode : public Node {
|
||||
private:
|
||||
int op;
|
||||
Node *first;
|
||||
Node *second;
|
||||
public:
|
||||
OpNode(int, Node*, Node*);
|
||||
virtual void display();
|
||||
};
|
||||
|
||||
class NodeList : public std::vector<Node*>{};
|
||||
|
||||
void print_expression_list(NodeList *);
|
||||
|
||||
#endif /* SYNTAX_H */
|
||||
Reference in New Issue
Block a user