Skip to content

Commit

Permalink
add log framework and new grammar definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
enkerewpo committed Aug 5, 2024
1 parent 94919b7 commit c7cb389
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 33 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ Cargo.lock
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
#.idea/

.vscode
8 changes: 5 additions & 3 deletions rasynth/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
[package]
name = "rasynth"
version = "0.1.0"
authors = ["wheatfox <[email protected]>"]
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies] # <-- We added this and everything after!
[build-dependencies]
lalrpop = "0.20.2"

[dependencies]
env_logger = "0.11.5"
graph = "0.3.1"
lalrpop-util = { version = "0.20.2", features = ["lexer", "unicode"] }
log = "0.4.22"
23 changes: 17 additions & 6 deletions rasynth/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@ pub enum Numeric {
}

#[derive(Debug, Clone)]
pub enum LetDefine {
pub enum LetDef {
Let(String, Expr),
}

#[derive(Debug, Clone)]
pub enum BoxDefine {
Box(String, Vec<Port>, Vec<Statement>),
pub enum BoxWire {
Boxw(String, Vec<Expr>),
}

#[derive(Debug, Clone)]
pub enum Statement {
LetDefine(LetDefine),
pub enum BoxDef {
Box(String, Vec<Port>, Vec<Stmt>),
}

#[derive(Debug, Clone)]
pub enum Stmt {
LetDef(LetDef),
BoxWire(BoxWire),
}

#[derive(Debug, Clone)]
Expand All @@ -48,4 +54,9 @@ impl Type {
_ => None,
}
}
}
}

#[derive(Debug, Clone)]
pub enum TopDef {
Boxes(Vec<BoxDef>),
}
26 changes: 26 additions & 0 deletions rasynth/src/graph.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::ast;
use log::*;
use std::sync::Mutex;

pub static FLOW_GRAPH: Mutex<Option<FlowGraph>> = Mutex::new(None);

#[derive(Debug)]
pub struct FlowGraph {
pub timestamp: u64,
pub ast: Mutex<Option<ast::TopDef>>,
}

impl FlowGraph {
pub fn new(ast: Option<ast::TopDef>) -> Self {
FlowGraph {
timestamp: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs(),
ast: Mutex::new(ast),
}
}
pub fn generate(&self) {
info!("Generating Graph...");
}
}
37 changes: 34 additions & 3 deletions rasynth/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
use env_logger::Env;
use lalrpop_util::lalrpop_mod;
use log::*;
use std::fs;

pub mod ast;
pub mod graph;

lalrpop_mod!(pub raslisp); // synthesized by LALRPOP

fn main() {
let test1 = fs::read_to_string("../test/test.raslisp").unwrap();
let r = raslisp::BoxDefineParser::new().parse(&test1).unwrap();
println!("{:?}", r);
let env = Env::default()
.filter_or("MY_LOG_LEVEL", "trace")
.write_style_or("MY_LOG_STYLE", "always");
env_logger::init_from_env(env);

info!("RASLISP Interpreter, version {}", env!("CARGO_PKG_VERSION"));
info!("Author: {}", env!("CARGO_PKG_AUTHORS"));

let input_file = "../test/osc1.raslisp";
info!("Input Top File Path: {}", input_file);
let test1 = fs::read_to_string(input_file).expect("Unable to read file");

let r = raslisp::TopParser::new().parse(&test1).unwrap();
info!("AST Parsed Successfully!");

graph::FLOW_GRAPH
.lock()
.unwrap()
.replace(graph::FlowGraph::new(Some(r)));

graph::FLOW_GRAPH
.lock()
.unwrap()
.as_ref()
.unwrap()
.generate();

info!("Graph: {:?}", graph::FLOW_GRAPH.lock().unwrap());

info!("Goodbye!");
}
50 changes: 41 additions & 9 deletions rasynth/src/raslisp.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,46 @@ use crate::ast::*;

grammar;

pub BoxDefine: BoxDefine = {
"(" "box" <name:NodeIdent> "(" <ports:PortVec> ")" <body:Exprs> ")" => BoxDefine::Box(name, ports, body),
match {
r"\s*" => { },
r";[^\n\r]*[\n\r]*" => { },
} else {
_
}

pub Top: TopDef = {
<bs:Boxes> => TopDef::Boxes(bs),
};

pub Boxes: Vec<BoxDef> = {
<b:BoxDef> => vec![b],
<b:BoxDef> <bs:Boxes> => {
let mut v = bs;
v.insert(0, b);
v
},
};

pub BoxDef: BoxDef = {
"(" "box" <name:NodeIdent> "(" <ports:PortVec> ")" <body:Stmts> ")" => BoxDef::Box(name, ports, body),
};

pub Exprs: Vec<Statement> = {
<s:Statement> => vec![s],
<s:Statement> <ss:Exprs> => {
pub Stmts: Vec<Stmt> = {
<s:Stmt> => vec![s],
<s:Stmt> <ss:Stmts> => {
let mut v = ss;
v.insert(0, s);
v
},
};

pub Statement: Statement = {
<ld:LetDefine> => Statement::LetDefine(ld),
pub Exprs: Vec<Expr> = {
<s:Expr> => vec![s],
<s:Expr> <ss:Exprs> => {
let mut v = ss;
v.insert(0, s);
v
},
};

pub PortVec: Vec<Port> = {
Expand All @@ -34,8 +59,15 @@ pub Port: Port = {
"out" <name:NodeIdent> ":" <ty:Type> => Port::Out(name, ty),
};

pub LetDefine: LetDefine = {
"(" "let" <name:NodeIdent> <expr:Expr> ")" => LetDefine::Let(name, expr),
pub Stmt: Stmt = {
<ld:LetDef> => Stmt::LetDef(ld),
<bw:BoxWire> => Stmt::BoxWire(bw),
};
pub BoxWire: BoxWire = {
"[" <name:NodeIdent> <exprs:Exprs> "]" => BoxWire::Boxw(name, exprs),
};
pub LetDef: LetDef = {
"(" "let" <name:NodeIdent> <expr:Expr> ")" => LetDef::Let(name, expr),
};
pub Expr: Expr = {
<ni:NodeIdent> => Expr::NodeIdent(ni),
Expand Down
21 changes: 11 additions & 10 deletions test/osc1.raslisp
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
[box osc1 (
(box osc1 (
in freq: float
in amp: float
in wav_sel: i32
out raw_wav: float
)
(let sin1 (sinwave 128)) // sin1: waveform
(let sample (idx sin1 65)) // get sin1[65]'s sample value
(let raw_wav amp * sample)
]
(let sin1 (sinwave 128)) ; sin1: waveform
(let sample (idx sin1 65)) ; get sin1[65]'s sample value
(let raw_wav (* amp sample))
)

[box main (
(box main (
out stereo_L: float
out stereo_R: float
)
[osc1 osc1_out_raw]
(let stereo_L = osc1_out_raw)
(let stereo_R = osc1_out_raw)
]
(let amp (* 50 2))
[osc1 440 amp 0 osc1_out_raw]
(let stereo_L osc1_out_raw)
(let stereo_R osc1_out_raw)
)
11 changes: 10 additions & 1 deletion test/test.raslisp
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,14 @@
in in1: float
out out1: float
)
(let out1 (+ in1 1))
(let out1 (+ in1 1)) ; comment test
)

(box main (
out L: float
out R: float
)
[test1 1 out1]
(let L out1)
(let R out1)
)

0 comments on commit c7cb389

Please sign in to comment.