Compare commits

..

29 Commits

Author SHA1 Message Date
7397ecc84b Nitpicks 2024-11-18 22:15:52 -06:00
b5ee47d2c8 Drop specific operators as terms, "Binary" or "Unary" ops only
This preps for Order of Operations
2024-11-18 22:02:33 -06:00
782c1c93f0 Drop REPL (George Bush voice) "for now" 2024-11-18 21:49:43 -06:00
488d7b1edb more cleanup 2024-11-18 21:45:10 -06:00
f2b0e38464 Audit other files for desired features 2024-11-18 21:42:17 -06:00
1e2c107c1b Remove no longer desired features from example 2024-11-18 21:36:24 -06:00
c29d463adf Clippy pass 2024-11-18 21:25:06 -06:00
5f57c5aed3 Saner munging 2024-11-18 21:21:25 -06:00
c345ac4fe5 Finish emitter 2024-11-18 21:16:02 -06:00
7f241523d4 Use new looping construct in examples 2024-11-18 10:41:51 -06:00
08cab74fb5 Add some display for each, order of operations won't be correct without context 2024-11-18 10:16:36 -06:00
c9295b5777 Add loop test 2024-11-16 14:29:02 -06:00
03b140189c Remove interpreter & evaluator 2024-11-16 13:50:20 -06:00
7213f4a487 Beginnings of loops 2024-11-16 13:44:04 -06:00
5fdd28fe0e Ackermann function 2024-11-14 15:49:37 -06:00
74f1fdc5ea Slightly change call semantics, add looping construct, inline some JS 2024-11-14 15:32:46 -06:00
9291e65c11 Use injected js, trampolines for curried functions 2024-11-14 13:09:54 -06:00
d0de70eb54 spacing 2024-11-14 12:46:48 -06:00
26bff70b1f Remove objects, add some other stuff so that I can make fizzbuzz run in node 2024-11-13 20:40:40 -06:00
8259b57a46 Change the way blocks are handled, i.e. not expressions but something else 2024-11-13 11:13:09 -06:00
0524efef03 Rip out objects 2024-11-13 10:43:00 -06:00
25fe7933f2 Add ideal peg as text file 2024-11-13 10:38:46 -06:00
d4d4756002 WIP to think about tail calls etc 2024-11-13 09:50:44 -06:00
201d7d2594 This is a rust project now, let's end the segregation 2024-11-12 13:56:37 -06:00
0fddcbd645 Beginnings of EMCAScript code generator 2024-11-12 07:19:43 -06:00
dea529b42b Fix up guarded block stuff (it's a statement) 2024-11-04 12:27:11 -06:00
3d8e76c9b0 Add dee-mode.el installation to makefile 2024-11-04 10:24:51 -06:00
69ccf679e7 WIP bc I forgot what I was doing 2024-11-04 10:07:12 -06:00
5436a706d7 Get/Set variables from the environment 2022-06-01 16:35:35 -05:00
23 changed files with 475 additions and 566 deletions

5
.gitignore vendored
View File

@@ -1,4 +1 @@
deelang
*.o
parser.h
lexer.h
target/*

View File

View File

@@ -1,7 +1,11 @@
.PHONY: all clean
.PHONY: all clean install
all:
$(MAKE) -C src
mv src/deelang .
clean:
$(MAKE) -C src clean
rm -f deelang
install:
install -t /usr/share/emacs/site-lisp/ dee-mode.el
uninstall:
rm -f /usr/share/emacs/site-lisp/dee-mode.el

View File

@@ -2,12 +2,6 @@
(add-to-list 'auto-mode-alist '("\\.dee\\'" . dee-mode))
(defface dee-function-param-face
'((t . (:slant italic
:weight bold
:foreground "orange")))
"A face to help identify function parameter in Deelang.")
(defcustom dee-indent-offset 4
"Default indentation offset for Deelang."
:group 'dee
@@ -34,14 +28,14 @@
(defvar dee-font-lock-keywords
`(,(rx symbol-start
(or
"if" "elif" "else" "")
"if" "elif" "else" "loop" "until" "over" "in" "")
symbol-end)
(,(rx (group (regexp dee-font-lock-id-re)) (* space) "<-")
(1 font-lock-variable-name-face))
(,(rx (group (regexp dee-font-lock-id-re)) (* space) "->")
(1 'dee-function-param-face))
(,(rx (group (regexp dee-font-lock-id-re)) (* space) "(")
(1 font-lock-function-name-face))))
(1 font-lock-function-name-face))
(,(rx (group (regexp dee-font-lock-id-re)) (* space) "->")
(1 font-lock-constant-face))))
(defun dee-indent-line-function ()
'noindent) ;TODO

View File

@@ -2,7 +2,6 @@ fun <- ->
do-something()
do-something-else()
foo <- ->
bar <- ->
baz <- ->
@@ -11,8 +10,7 @@ foo <- ->
foo() ## Inside foo but not in baz or bar
add <- x -> y ->
return x + y ## Multivariable functions by currying
add <- x -> y -> x + y ## Multivariable functions by currying
add(x) ## Returns a _function_
add(x, y) ## This is equivalent to add(x)(y)
add(x, y) ## This is equivalent to add(x)(y)

View File

@@ -7,7 +7,7 @@ foo <- -> bar() ## function definitions
foo <- ->
bar()
baz()
## functions with block statements
# functions with block statements
foo <- x -> y -> x * y + 7 ## Multiple argument functions
if test?()
do-something()
@@ -24,8 +24,8 @@ else
do-a-real-bad-thing()
## Nested blocks
obj <- {
field <- "hello"
method <- x -> x * 2
}
## Object creation
i <- 1
loop until i > 10
print(i)
i <- 1
## Loops

View File

@@ -1,24 +1,11 @@
## By the end of the book I want to have an interpreter
## for this little language, maybe a compiler too
print(1 + 1) ## 2
print("Hello world!") ## Hello world
name <- read()
print("Hi there $name") ## Hi there <name>
have <- 10
want <- 11
need <- have - want ## This is subtraction
have-want <- 1 ## This is a variable named "have-want"
print(HaVe-WAnt) ## 1 (case doesn't matter)
%%option meaningful-casing
Apples <- 1
print(apples) ## <undef> (or maybe it does)
print("one fish"); print("two fish") ## Two statements on the same line
say-hi <- -> print("Hi from inside a function")
say-hi() ## Hi from inside a function
@@ -31,8 +18,8 @@ duck <- {
eats? <- food -> food = "bread"
} ## Objects created on the fly
print duck.eats?("bread") ## true
print duck.eats?("corn") ## false
print(duck.eats?("bread")) ## true
print(duck.eats?("corn")) ## false
cow <- {
talk <- print("Moo")
@@ -43,15 +30,15 @@ human <- {
eats? <- _ -> true
}
print cow.eats?("grass") ## true
print cow.eats?("corn") ## true
print(cow.eats?("grass")) ## true
print(cow.eats?("corn")) ## true
talk-or-pass-wind <- character ->
if character has talk then
if character has talk
character.talk()
else
print("*Fart*")
talk-or-pass-wind(duck) ## "Quack"
talk-or-pass-wind(cow) ## "Moo"
talk-or-pass-wind(human) ## "*Fart*"
talk-or-pass-wind(human) ## "*Fart*"

18
demo/real.dee Normal file
View File

@@ -0,0 +1,18 @@
## Obligatory
fizzbuzz <- ->
loop over x in range(100)
if x % 15 = 0 print("fizzbuzz")
elif x % 3 = 0 print("fizz")
elif x % 5 = 0 print("buzz")
else print(x)
fizzbuzz()
print()
ack <- m -> n ->
if m = 0 n + 1
elif n = 0 ack(m - 1, 1)
else ack(m - 1, ack(m, n - 1))
print(ack(3, 4)) ## 125

1
rust/.gitignore vendored
View File

@@ -1 +0,0 @@
target/*

View File

@@ -1,110 +0,0 @@
use std::ops;
use std::collections::HashMap;
use crate::parser;
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 eval(ast: &parser::Stmt, env: &mut Env) {
match &ast {
parser::Stmt::ReplPrint(expr) =>
println!("{}", eval_expr(expr, 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!"),
}
}
}

View File

@@ -1,52 +0,0 @@
mod parser;
mod evaluator;
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,
}
fn repl(cli: &Cli) {
let mut global = evaluator::Env::global();
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 {
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 {
todo!();
}
}
fn main() {
let cli = Cli::parse();
match cli.file {
None => repl(&cli),
Some(_) => script(&cli),
}
}

View File

@@ -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

View File

@@ -1,11 +0,0 @@
#include "syntax.h"
#include "lexer.h"
#include "parser.h"
using namespace std;
int main() {
yyparse();
return 0;
}

191
src/emitter.rs Normal file
View File

@@ -0,0 +1,191 @@
use std::io::Write;
use std::collections::HashSet;
use crate::parser;
fn munge(s: &str) -> String {
s.replace("?", "p").replace("-", "_")
}
pub struct LexicalContext<'a> {
parent: Option<&'a LexicalContext<'a>>,
local: HashSet<String>,
is_tail: bool,
}
impl <'a> LexicalContext<'a> {
pub fn toplevel() -> Self {
LexicalContext {
parent: None,
local: HashSet::new(),
is_tail: false,
}
}
fn new(parent: &'a LexicalContext<'a>) -> Self {
LexicalContext {
parent: Some(parent),
local: HashSet::new(),
is_tail: parent.is_tail,
}
}
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)
}
}
/// Embeds the pre-written js
pub fn emit_injector(w: &mut dyn Write) -> std::io::Result<()>{
let bytes = include_bytes!("./js/deeinject.js");
write!(w, "{}", String::from_utf8_lossy(bytes))?;
Ok(())
}
pub fn emit_all(w: &mut dyn Write, ast: &[parser::Stmt], ctx: &mut LexicalContext) -> std::io::Result<()> {
let is_tail = ctx.is_tail;
ctx.is_tail = false;
if let Some((last, butlast)) = ast.split_last() {
for stmt in butlast {
emit(w, stmt, ctx)?;
}
ctx.is_tail = is_tail;
emit(w, last, ctx)?;
}
Ok(())
}
pub fn emit(w: &mut dyn Write, stmt: &parser::Stmt, ctx: &mut LexicalContext) -> std::io::Result<()> {
match &stmt {
parser::Stmt::BareExpr(expr) => {
if ctx.is_tail {
write!(w, "return ")?;
}
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, "{} = ", munge(id))?;
emit_expr(w, expr, ctx)?;
writeln!(w, ";")?;
if ctx.is_tail {
writeln!(w, "return;")?;
}
}
parser::Stmt::Conditional(if_blocks, else_block) => {
let mut first_block = true;
for if_block in if_blocks {
if first_block {
write!(w, "if (")?;
} else {
write!(w, "else if(")?;
}
first_block = false;
emit_expr(w, &if_block.guard, ctx)?;
writeln!(w, ") {{")?;
let mut block_ctx = LexicalContext::new(ctx);
emit_all(w, &if_block.block, &mut block_ctx)?;
writeln!(w, "}}")?;
}
if let Some(block) = else_block {
writeln!(w, "else {{")?;
let mut block_ctx = LexicalContext::new(ctx);
emit_all(w, block, &mut block_ctx)?;
writeln!(w, "}}")?;
}
}
parser::Stmt::Loop(eloop) => {
let is_tail = ctx.is_tail;
ctx.is_tail = false;
match &eloop {
parser::Loop::Over(id, expr, block) => {
write!(w, "for (let {} of ", id)?;
emit_expr(w, expr, ctx)?;
writeln!(w, ") {{")?;
let mut block_ctx = LexicalContext::new(ctx);
emit_all(w, block, &mut block_ctx)?;
writeln!(w, "}}")?;
ctx.is_tail = is_tail;
}
parser::Loop::Until(guarded_block) => {
write!(w, "while (!(")?;
emit_expr(w, &guarded_block.guard, ctx)?;
write!(w, ")) {{")?;
let mut block_ctx = LexicalContext::new(ctx);
emit_all(w, &guarded_block.block, &mut block_ctx)?;
writeln!(w, "}}")?;
}
parser::Loop::Bare(block) => {
write!(w, "for(;;){{")?;
let mut block_ctx = LexicalContext::new(ctx);
emit_all(w, block, &mut block_ctx)?;
writeln!(w, "}}")?;
}
}
ctx.is_tail = is_tail;
}
}
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, "{}", munge(id))?;
}
parser::Expr::Atom(atom) => {
write!(w, "{}", atom)?;
}
parser::Expr::Funcall(id, args) => {
write!(w, "{}", munge(id))?;
if args.is_empty() {
write!(w, "()")?;
} else if ctx.contains(id) {
// Use deelang calling semantics
for arg in args {
write!(w, "(")?;
emit_expr(w, arg, ctx)?;
write!(w, ")")?
}
} else {
// Use JS calling semantics
write!(w, "(")?;
let (last, butlast) = args.split_last().unwrap();
for arg in butlast {
emit_expr(w, arg, ctx)?;
write!(w, ",")?
}
emit_expr(w, last, ctx)?;
write!(w, ")")?;
}
}
parser::Expr::Funcdef(arg, body) => {
if let Some(arg) = arg {
writeln!(w, "function ({}){{", arg)?;
} else {
writeln!(w, "function (){{")?;
}
let mut fun_ctx = LexicalContext::new(ctx);
fun_ctx.is_tail = true;
emit_all(w, body, &mut fun_ctx)?;
write!(w, "}}")?;
}
parser::Expr::BinaryOp(op, e1, e2) => {
emit_expr(w, e1.as_ref(), ctx)?;
write!(w, " {} ", op)?;
emit_expr(w, e2.as_ref(), ctx)?;
}
parser::Expr::UnaryOp(op, e) => {
write!(w, "{}(", op)?;
emit_expr(w, e.as_ref(), ctx)?;
write!(w, ")")?;
}
}
Ok(())
}

3
src/internal.rs Normal file
View File

@@ -0,0 +1,3 @@
//! Internal compiler steps, expansion, cps, optimization.

10
src/js/deeinject.js Normal file
View File

@@ -0,0 +1,10 @@
const print = console.log
function range(x) {
const ret = []
for (let i = 0; i < x; i++) {
ret.push(i);
}
return ret
}

View File

@@ -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);
%%

43
src/main.rs Normal file
View File

@@ -0,0 +1,43 @@
mod parser;
mod emitter;
mod internal;
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 = "Compiler for Deelang")]
struct Cli {
#[clap(help="Specify a file to compile")]
file: PathBuf,
#[clap(short, long, help="Emit a parse tree")]
parse: bool,
#[clap(short, long, help="Cross compile to ECMAScript")]
ecmascript: bool,
#[clap(long, help="Run the preprocessor")]
preprocess: bool,
}
fn main() {
let cli = Cli::parse();
let mut file = File::open(&cli.file).expect("Couldn't read file");
let mut prgm = String::new();
file.read_to_string(&mut prgm).unwrap();
if cli.preprocess {
println!("{}", parser::preprocess(&prgm));
return;
}
let tree = parser::parse(&prgm);
if cli.parse {
println!("{:#?}", tree);
} else if cli.ecmascript {
let mut out = io::stdout();
emitter::emit_injector(&mut out).ok();
let mut toplevel = emitter::LexicalContext::toplevel();
emitter::emit_all(&mut out, &tree, &mut toplevel).ok();
}
}

View File

@@ -3,9 +3,9 @@ use std::fmt;
#[derive(Debug,PartialEq,Clone)]
pub enum Stmt {
Assignment(String, Expr),
Funcall(Expr),
Conditional(Vec<Expr>, Option<Expr>),
ReplPrint(Expr),
Conditional(Vec<GuardedBlock>, Option<Block>),
Loop(Loop),
BareExpr(Expr),
}
#[derive(Debug,PartialEq,Clone)]
@@ -13,36 +13,31 @@ 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>),
GuardedBlock(Box<GuardedBlock>),
Object(Vec<Stmt>),
Funcdef(Option<String>, Block),
UnaryOp(String, Box<Expr>),
BinaryOp(String, Box<Expr>, Box<Expr>),
}
pub type Block = Vec<Stmt>;
#[derive(Debug,PartialEq,Clone)]
pub enum Atom {
String(String),
Num(f64),
}
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),
}
}
Bool(bool),
}
#[derive(Debug,PartialEq,Clone)]
pub struct GuardedBlock {
pub guard: Expr,
pub block: Vec<Stmt>,
pub block: Block,
}
#[derive(Debug,PartialEq,Clone)]
pub enum Loop {
Bare(Block),
Until(GuardedBlock),
Over(String, Expr, Block),
}
peg::parser! {
@@ -51,62 +46,80 @@ peg::parser! {
= __* 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) }
l:_loop() { Stmt::Loop(l) } /
e:expr() stop() { Stmt::BareExpr(e) }
rule expr() -> Expr = precedence! {
"-" _ e1:@ { Expr::UnaryMinus(Box::new(e1)) }
e1:(@) "=" _ e2:@ { Expr::BinaryOp("=".to_string(), Box::new(e1), Box::new(e2))}
--
e1:(@) r:relop() e2:@ { Expr::BinaryOp(r, Box::new(e1), Box::new(e2)) }
--
"-" _ e1:@ { Expr::UnaryOp("-".to_string(), 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::BinaryOp("+".to_string(), Box::new(e1), Box::new(e2)) }
e1:(@) "-" _ e2:@ { Expr::BinaryOp("-".to_string(), 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)) }
e1:(@) "*" _ e2:@ { Expr::BinaryOp("*".to_string(), Box::new(e1), Box::new(e2)) }
e1:(@) "/" _ e2:@ { Expr::BinaryOp("/".to_string(), Box::new(e1), Box::new(e2)) }
e1:(@) "%" _ e2:@ { Expr::BinaryOp("%".to_string(), Box::new(e1), Box::new(e2)) }
--
"(" _ e:expr() ")" _ { e }
['"'] s:$((!['"'] [_] / r#"\""#)*) ['"'] { Expr::Atom(Atom::String(s.to_string())) }
f:funcall() { f }
f:funcdef() { f }
o:object() { o }
b:boolean() _ { Expr::Atom(Atom::Bool(b)) }
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 relop() -> String
= r:$("<" / ">" / "<=" / ">=") _ { r.to_string() }
rule num() -> f64
= n:$(digit()+ "."? digit()* / "." digit()+) _ { n.parse().unwrap() }
rule funcall() -> Expr
= i:id() "(" _ e:(expr() ** ("," _)) ")" _ { Expr::Funcall(i, e) }
= i:id() "(" _ e:(expr() ** ("," _)) __* ")" _ { Expr::Funcall(i, e) }
rule funcdef() -> Expr
= i:id()? "->" _ e:(expr() / block()) { Expr::Funcdef(i, Box::new(e)) }
= i:id()? "->" _ b:(block()) { Expr::Funcdef(i, b) }
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() -> Expr
= "if" _ g:expr() b:indented_block() {
Expr::GuardedBlock(Box::new(GuardedBlock {
= i:_if() __* ei:elif()* e:_else()? __* { Stmt::Conditional([vec![i], ei].concat(), e) }
rule _if() -> GuardedBlock
= "if" _ g:expr() b:block() {
GuardedBlock {
guard: g,
block: b,
}))
}
}
rule elif() -> Expr
= "elif" _ g:expr() b:indented_block() {
Expr::GuardedBlock(Box::new(GuardedBlock {
rule elif() -> GuardedBlock
= "elif" _ g:expr() b:block() __* {
GuardedBlock {
guard: g,
block: b
}))
}
}
rule _else() -> Expr
rule _else() -> Block
= "else" _ b:block() { b }
rule block() -> Expr
= i:indented_block() { Expr::Block(i) }
rule indented_block() -> Vec<Stmt>
rule _loop() -> Loop
= "loop" _ "until" _ e:expr() b:block() __* {
Loop::Until(GuardedBlock{
guard:e,
block: b,
})
} /
"loop" _ "over" _ i:id() "in" _ e:expr() b:block() __* {
Loop::Over(i, e, b)
} /
"loop" _ b:block() __* { Loop::Bare(b) }
rule block() -> Block
= i:indented_block() { i } /
e:expr() { vec![Stmt::BareExpr(e)] }
rule indented_block() -> Block
= stop() indent() __* s:stmt()+ dedent() { s }
rule letter()
@@ -132,7 +145,7 @@ peg::parser! {
}
}
fn preprocess(input: &str) -> String {
pub fn preprocess(input: &str) -> String {
let mut stack = vec![0];
let mut output = String::new();
for line in input.lines() {
@@ -165,6 +178,12 @@ fn preprocess(input: &str) -> String {
output.push_str(line.trim());
output.push('\n');
}
while stack.len() > 1 {
// place any dedents needed to balance the program
output.push_str("<<<");
output.push('\n');
stack.pop();
}
output
}
@@ -173,9 +192,77 @@ pub fn parse(prgm: &str) -> Vec<Stmt> {
deelang_parser::program(&prgm).unwrap()
}
pub fn parse_stmt(stmt: &str) -> Stmt {
let stmt = preprocess(stmt);
deelang_parser::stmt(&stmt).unwrap()
impl fmt::Display for Stmt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Stmt::Assignment(id, expr) => writeln!(f, "{} <- {}", id, expr),
Stmt::BareExpr(expr) => writeln!(f, "{}", expr),
Stmt::Conditional(guarded_blocks, else_block) => {
let (if_block, elif_blocks) = guarded_blocks.split_first().unwrap();
write!(f, "if {}", if_block.guard)?;
write_block(f, &if_block.block)?;
for elif_block in elif_blocks {
write!(f, "elif {}", elif_block.guard)?;
write_block(f, &if_block.block)?;
}
if let Some(block) = else_block {
write!(f, "else")?;
write_block(f, block)?;
}
Ok(())
}
Stmt::Loop(loop_inner) => match &loop_inner {
Loop::Bare(block) => {
write!(f, "loop")?;
write_block(f, block)
}
Loop::Over(id, pred, block) => {
write!(f, "loop over {} in {}", id, pred)?;
write_block(f, block)
}
Loop::Until(guarded_block) => {
write!(f, "loop until {}", guarded_block.guard)?;
write_block(f, &guarded_block.block)
}
},
}
}
}
fn write_block (f: &mut fmt::Formatter<'_>, block: &Block) -> fmt::Result {
if block.len() == 1 {
writeln!(f, " {}", block[0])
} else {
writeln!(f)?;
writeln!(f, ">>>")?;
for stmt in block {
write!(f, "{}", stmt)?;
}
writeln!(f, "<<<")
}
}
impl fmt::Display for Expr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expr::Atom(a) => write!(f, "{}", a),
Expr::Id(id) => write!(f, "{}", id),
Expr::BinaryOp(op, e1, e2) => write!(f, "{} {} {}", e1, op, e2),
Expr::UnaryOp(op, e) => write!(f, "{} {}", op, e),
Expr::Funcdef(_arg, _block) => todo!(),
Expr::Funcall(_id, _args) => todo!(),
}
}
}
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),
}
}
}
@@ -195,8 +282,8 @@ apple <- 1 ## This is too
#[test]
fn test_funcall() {
let expected = vec![
Stmt::Funcall(Expr::Funcall("pear".to_string(), vec![])),
Stmt::Funcall(Expr::Funcall("pear".to_string(),
Stmt::BareExpr(Expr::Funcall("pear".to_string(), vec![])),
Stmt::BareExpr(Expr::Funcall("pear".to_string(),
vec![Expr::Id("x".to_string()), Expr::Id("y".to_string())],
))
];
@@ -229,18 +316,22 @@ one <- 3 - 2
four <- (3 - 1) * 2";
let expected = vec![
Stmt::Assignment("three".to_string(),
Expr::Plus(
Expr::BinaryOp(
"+".to_string(),
Box::new(Expr::Atom(Atom::Num(1.0))),
Box::new(Expr::Atom(Atom::Num(2.0))),
)),
Stmt::Assignment("one".to_string(),
Expr::Minus(
Expr::BinaryOp(
"-".to_string(),
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(
Expr::BinaryOp(
"*".to_string(),
Box::new(Expr::BinaryOp(
"-".to_string(),
Box::new(Expr::Atom(Atom::Num(3.0))),
Box::new(Expr::Atom(Atom::Num(1.0))),
)),
@@ -255,7 +346,8 @@ four <- (3 - 1) * 2";
let prgm = "apple <- pear(x, y) + z";
let expected = vec![
Stmt::Assignment("apple".to_string(),
Expr::Plus(
Expr::BinaryOp(
"+".to_string(),
Box::new(Expr::Funcall(
"pear".to_string(),
vec![Expr::Id("x".to_string()), Expr::Id("y".to_string())],
@@ -281,36 +373,37 @@ foo <- x -> y -> x * y";
"foo".to_string(),
Expr::Funcdef(
None,
Box::new(Expr::Funcall("bar".to_string(), vec![])),
)
vec![Stmt::BareExpr(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![])),
]))
)
vec![
Stmt::BareExpr(Expr::Funcall("bar".to_string(), vec![])),
Stmt::BareExpr(Expr::Funcall("baz".to_string(), vec![])),
],
)
),
Stmt::Assignment(
"foo".to_string(),
Expr::Funcdef(
Some("x".to_string()),
Box::new(
Expr::Funcdef(
vec![
Stmt::BareExpr(Expr::Funcdef(
Some("y".to_string()),
Box::new(
Expr::Mult(
vec![
Stmt::BareExpr(Expr::BinaryOp(
"*".to_string(),
Box::new(Expr::Id("x".to_string())),
Box::new(Expr::Id("y".to_string())),
)
)
)
)
))
],
))
],
)
)
),
];
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
}
@@ -327,36 +420,32 @@ else
<<<";
let expected = vec![Stmt::Conditional(
vec![
Expr::GuardedBlock(Box::new(GuardedBlock {
GuardedBlock {
guard: Expr::Id("foo".to_string()),
block: vec![Stmt::Funcall(Expr::Funcall("bar".to_string(), vec![]))]
})),
Expr::GuardedBlock(Box::new(GuardedBlock {
block: vec![Stmt::BareExpr(Expr::Funcall("bar".to_string(), vec![]))]
},
GuardedBlock {
guard: Expr::Id("baz".to_string()),
block: vec![Stmt::Funcall(Expr::Funcall("foobar".to_string(), vec![]))]
})),
block: vec![Stmt::BareExpr(Expr::Funcall("foobar".to_string(), vec![]))],
},
],
Some(Expr::Block(vec![Stmt::Funcall(Expr::Funcall("quux".to_string(), vec![]))])),
Some(vec![Stmt::BareExpr(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);
fn test_loop() {
let prgm = r"loop until i > 100 a";
let expected = vec![Stmt::Loop(Loop::Until(GuardedBlock {
guard: Expr::BinaryOp(
">".to_string(),
Box::new(Expr::Id("i".to_string())),
Box::new(Expr::Atom(Atom::Num(100.0))),
),
block: vec![Stmt::BareExpr(Expr::Id("a".to_string()))],
}))];
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
}
#[test]
fn test_preprocess() {
let prgm = r"

View File

@@ -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
%%

View File

@@ -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();
}
}

View File

@@ -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 */