Skip to content

Commit

Permalink
smol refactor + web init
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubhamai committed Apr 23, 2024
1 parent a3142af commit 5666046
Show file tree
Hide file tree
Showing 25 changed files with 5,500 additions and 163 deletions.
79 changes: 79 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ description = "A programming language with built-in autograd"
[dependencies]
clap = { version = "4.5.4", features = ["derive"] }
logos = "0.14.0"
wasm-bindgen = "0.2"

[lib]
crate-type = ["cdylib"]
35 changes: 7 additions & 28 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub enum UnaryOp {
pub enum PostfixOp {
Index,
Call,
STAR_STAR, // exponentiation
StarStar, // exponentiation
}

#[derive(Debug, Clone, Copy, PartialEq)]
Expand Down Expand Up @@ -149,7 +149,7 @@ impl fmt::Display for Ops {

Ops::PostfixOp(PostfixOp::Index) => write!(f, "["),
Ops::PostfixOp(PostfixOp::Call) => write!(f, "."),
Ops::PostfixOp(PostfixOp::STAR_STAR) => write!(f, "**"),
Ops::PostfixOp(PostfixOp::StarStar) => write!(f, "**"),
// Ops::PostfixOp(PostfixOp::Args) => write!(f, ","),
}
}
Expand Down Expand Up @@ -235,7 +235,7 @@ fn expr_bp(lexer: &mut Lexer, min_bp: u8) -> ASTNode {
TokenType::GREATER_EQUAL => Ops::BinaryOp(BinaryOp::Ge),

TokenType::DOT => Ops::PostfixOp(PostfixOp::Call),
TokenType::STAR_STAR => Ops::PostfixOp(PostfixOp::STAR_STAR),
TokenType::STAR_STAR => Ops::PostfixOp(PostfixOp::StarStar),

TokenType::BANG => Ops::UnaryOp(UnaryOp::Not),

Expand Down Expand Up @@ -268,7 +268,7 @@ fn expr_bp(lexer: &mut Lexer, min_bp: u8) -> ASTNode {

TokenType::DOT => Ops::PostfixOp(PostfixOp::Call),
TokenType::LEFT_BRACKET => Ops::PostfixOp(PostfixOp::Index),
TokenType::STAR_STAR => Ops::PostfixOp(PostfixOp::STAR_STAR),
TokenType::STAR_STAR => Ops::PostfixOp(PostfixOp::StarStar),

TokenType::BANG => Ops::UnaryOp(UnaryOp::Negate),

Expand Down Expand Up @@ -307,30 +307,9 @@ fn expr_bp(lexer: &mut Lexer, min_bp: u8) -> ASTNode {
}
}

// let mut args = HashMap::new();
// while lexer.peek().token_type != TokenType::RIGHT_PAREN {

// // let key = lexer.next().lexeme;
// // assert_eq!(lexer.next().token_type, TokenType::EQUAL);
// // let value = expr_bp(lexer, 0);
// // args.insert(key, value);

// if lexer.peek().token_type == TokenType::COMMA {
// lexer.next();
// }
// }

lexer.next();
ASTNode::Op(
op,
// vec![
// lhs,
// S::Identifier(callee_token.lexeme),
// S::Op(Ops::PostfixOp(PostfixOp::Args), args),
// ],
vec![lhs, ASTNode::Callee(callee_token.lexeme, args)],
)
} else if op == Ops::PostfixOp(PostfixOp::STAR_STAR) {
ASTNode::Op(op, vec![lhs, ASTNode::Callee(callee_token.lexeme, args)])
} else if op == Ops::PostfixOp(PostfixOp::StarStar) {
let rhs = expr_bp(lexer, 0);
ASTNode::Op(op, vec![lhs, rhs])
} else {
Expand Down Expand Up @@ -379,7 +358,7 @@ fn postfix_binding_power(op: Ops) -> Option<(u8, ())> {
// '[' => (11, ()),
Ops::PostfixOp(PostfixOp::Index) => (13, ()),
Ops::PostfixOp(PostfixOp::Call) => (14, ()),
Ops::PostfixOp(PostfixOp::STAR_STAR) => (16, ()),
Ops::PostfixOp(PostfixOp::StarStar) => (16, ()),
_ => return None,
};
Some(res)
Expand Down
13 changes: 9 additions & 4 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ impl Compiler {
Ops::BinaryOp(BinaryOp::Mul) => {
self.chunk.write(VectorType::Code(OpCode::OpMultiply))
}
// @ - dot product - TODO: need to implement
Ops::BinaryOp(BinaryOp::At) => {
self.chunk.write(VectorType::Code(OpCode::OpMultiply))
}
Ops::BinaryOp(BinaryOp::Div) => {
self.chunk.write(VectorType::Code(OpCode::OpDivide))
}
Expand Down Expand Up @@ -103,7 +107,7 @@ impl Compiler {
self.chunk.write(VectorType::Code(OpCode::OpNegate))
}

Ops::PostfixOp(PostfixOp::STAR_STAR) => {
Ops::PostfixOp(PostfixOp::StarStar) => {
self.chunk.write(VectorType::Code(OpCode::OpPower))
}
Ops::PostfixOp(PostfixOp::Call) => {
Expand All @@ -113,7 +117,7 @@ impl Compiler {
.write(VectorType::Constant(self.chunk.constants.len() - 1));
// TODO: need for testing for this - a.relu(c.relu()), a.relu().relu()
}
x => println!("Invalid operator {:?}", x),
Ops::UnaryOp(UnaryOp::Not) | Ops::PostfixOp(PostfixOp::Index) => todo!(),
}
}
ASTNode::Print(expr) => {
Expand Down Expand Up @@ -147,21 +151,22 @@ impl Compiler {
self.chunk.write(VectorType::Code(OpCode::OpSetGlobal));
self.chunk.write(VectorType::Constant(global));
}
ASTNode::Callee(iden, args) => {
ASTNode::Callee(iden, _) => {
println!("Callee");
let global = self
.chunk
.add_constant(ValueType::Identifier(self.interner.intern_string(iden)));
self.chunk.write(VectorType::Constant(global));

// TODO: need to implement args

// for arg in args {
// self.visit(arg.clone());
// }

// self.chunk.write(VectorType::Code(OpCode::OpCall));
// self.chunk.write(VectorType::Constant(args.len()));
}
_ => println!("Invalid ASTNode"), // TODO: handle this error
}
}
}
11 changes: 0 additions & 11 deletions src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,6 @@ pub struct Interner {
}

impl Interner {
pub fn intern(&mut self, name: &str) -> StringObjIdx {
if let Some(&idx) = self.map.get(name) {
return idx;
}
let idx = self.map.len() as StringObjIdx;
self.map.insert(name.to_owned(), idx);
self.vec.push(name.to_owned());

idx
}

pub fn intern_string(&mut self, name: String) -> StringObjIdx {
if let Some(&idx ) = self.map.get(&name) {
return idx;
Expand Down
35 changes: 35 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
mod ast;
mod chunk;
mod compiler;
mod debug;
mod interner;
mod scanner;
mod tensor;
mod value;
mod vm;

use crate::{ast::Parser, scanner::Lexer};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn run_source(src: &str) -> String {
let mut lexer = Lexer::new(src.to_string());

let out = Parser::new(&mut lexer).parse();
for stmt in out.iter() {
println!("{:?}", stmt);
}
println!("-------------");

let mut compiler = compiler::Compiler::new();
let (bytecode, interner) = compiler.compile(out);
println!("{:?}", bytecode);

let debug = debug::Debug::new("test", bytecode.clone());
debug.disassemble();

let mut vm = vm::VM::init(bytecode, interner);
let result = vm.run();

format!("{:?}", result)
}
Loading

0 comments on commit 5666046

Please sign in to comment.