Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
linsyking committed Apr 2, 2023
1 parent fa997c1 commit b9aa498
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 43 deletions.
91 changes: 76 additions & 15 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,88 @@
# meow~
# Meow lang

Another style:
```meow
true() { "T" }
Meow lang is a programming language that is designed to **not** be easy to learn and use.

Its mean design purpose is to do experiments with "string replacement".

## How to run

```bash
cargo run <your meow file path>

# Now you can use the REPL to evaluate expressions
```

Run the example code:

```
no error found.
> true()
T
> false()
F
> {var x = zero();succ(x)}
S0
> dr("a","aba","aba")
aba
> m()
abcd
```

## Examples

```meow
true() {"T"}
false() {"F"}
zero() {"0"}
succ(x) {"S" + x}
succ(x) {"S"+x}
if(c,y,x) {
var custom = y + "1";
"0" = x;
y + "1" = y;
true() = custom;
false() = "0";
c
pred(x) {
"S0" = "0";
x
}
succ2(x) {
succ(succ(x))
dr(x,y,z) {
y = x;
x = y;
z
}
rep_test() {
"dd" = "d";
var x = {"dddd"};
var y = "dddd";
"x=" + x + ",y=" + y
}
encode(s) {
var rep = {
"\" = "\\";
"$" = "\$";
"^" = "\^";
s
};
"_^"+ rep +"_$"
}
decode(s) {
var rep = {
"_^" = "";
"_$" = "";
s
};
"\$" = "$";
"\^" = "^";
"\\" = "\";
rep
}
```
m(){
var s = "abcd";
var enc = encode(s);
var dec = decode(enc);
dec
}
```
18 changes: 18 additions & 0 deletions docs/String.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# String Operation

Definition of replacement:

$[a/b]c := \begin{cases}
xa[a/b]y, \exist x, y,(b\not\subset x)\land (c=xby) \\
c, \text{otherwise}
\end{cases}$

Double Replacement Lemma:

$[x/y]([y/x]y)=\begin{cases}
y, x\subset y\\
x, \text{otherwise}
\end{cases}$

Similar result:
$[x/yy]([yy/x]y)=y$
59 changes: 52 additions & 7 deletions examples/syn.meow
Original file line number Diff line number Diff line change
@@ -1,9 +1,54 @@
hh() { "dd" }
sdf(hhs, fff) {
"fd" = hhg("ss");
var ldk = {
var s = ("ff" + hhs + "d");
"hhh"
true() {"T"}

false() {"F"}

zero() {"0"}

succ(x) {"S"+x}

pred(x) {
"S0" = "0";
x
}

dr(x,y,z) {
y = x;
x = y;
z
}

rep_test() {
"dd" = "d";
var x = {"dddd"};
var y = "dddd";
"x=" + x + ",y=" + y
}

encode(s) {
var rep = {
"\" = "\\";
"$" = "\$";
"^" = "\^";
s
};
"ffs"
"_^"+ rep +"_$"
}

decode(s) {
var rep = {
"_^" = "";
"_$" = "";
s
};
"\$" = "$";
"\^" = "^";
"\\" = "\";
rep
}

m(){
var s = "abcd";
var enc = encode(s);
var dec = decode(enc);
dec
}
7 changes: 4 additions & 3 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Stmt {
#[derive(Debug, Clone)]
pub enum Expr {
Literal(String),
Cat(Vec<Box<Expr>>),
Cat(Box<Expr>, Box<Expr>),
MacAp(MacAp),
Var(String),
Block(Box<Block>),
Expand All @@ -27,8 +27,9 @@ pub struct MacAp {
}

#[derive(Debug, Clone)]
pub enum Block {
BStmt(Vec<Box<BStmt>>, Box<Expr>),
pub struct Block {
pub bstmts: Vec<Box<BStmt>>,
pub expr: Box<Expr>,
}

#[derive(Debug, Clone)]
Expand Down
18 changes: 16 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::env::args;

pub mod ast;
mod ast;
mod prog;

#[macro_use]
extern crate lalrpop_util;
Expand All @@ -15,5 +16,18 @@ fn main() {
let res = parse::ProgramParser::new()
.parse(fc.as_str())
.expect("unexpected token!");
println!("{:?}", res);
let context = &mut Box::new(prog::Context::new());
prog::eval_prog(res, context);
loop {
let mut line = String::new();
eprint!("> ");
std::io::stdin().read_line(&mut line).unwrap();
line = line.trim().to_string();
let expr = parse::ExpressionParser::new()
.parse(line.as_str())
.expect("invalid expression!");
// Eval!
let res = prog::eval(&expr, context);
println!("{}", res);
}
}
26 changes: 10 additions & 16 deletions src/parse.lalrpop
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::str::FromStr;
use crate::ast::{Prog, Stmt, Expr, MacAp, Block, BStmt}; // (0)
// use lalrpop_util::ParseError;
use crate::ast::{Prog, Stmt, Expr, MacAp, Block, BStmt};

grammar;

Expand All @@ -14,15 +13,6 @@ Comma<T>: Vec<T> = {
}
};

AddListE = Add<Expression>;

Add<T>: Vec<T> = {
<mut v:(<T> "+")*> <e:T> => {
v.push(e);
v
}
};

pub Program: Box<Prog> = {
<s:Statement> <p:Program> => Box::new(Prog::Stmts(s, p)) ,
() => Box::new(Prog::Epsilon)
Expand All @@ -32,17 +22,21 @@ Statement: Stmt = {
<n:VarName> <a:ArgsVar> "{" <b:BlockE> "}" => Stmt{name: n, args: a, block: b}
}

Expression: Box<Expr> = {
pub Expression: Box<Expr> = {
<e:Expression> "+" <t:Term> => Box::new(Expr::Cat(e, t)),
Term
}

Term: Box<Expr> = {
Literal => Box::new(Expr::Literal(<>)),
"(" <AddListE> ")" => Box::new(Expr::Cat(<>)),
// "(" <e:Expression> ")" => e,
MacroAp => Box::new(Expr::MacAp(<>)),
VarName => Box::new(Expr::Var(<>)),
"{" <b:BlockE> "}" => Box::new(Expr::Block(b))
"{" <b:BlockE> "}" => Box::new(Expr::Block(b)),
"(" <e:Expression> ")" => e
}

BlockE: Box<Block> = {
<bs:(<BlockStmt> ";")*> <e:Expression> => Box::new(Block::BStmt(bs, e))
<bs:(<BlockStmt> ";")*> <e:Expression> => Box::new(Block{bstmts: bs, expr: e})
}

BlockStmt: Box<BStmt> = {
Expand Down
Loading

0 comments on commit b9aa498

Please sign in to comment.