Skip to content

Commit

Permalink
Merge pull request #2 from Shubhamai/wip
Browse files Browse the repository at this point in the history
Wip
  • Loading branch information
Shubhamai authored Jun 9, 2024
2 parents 1327b7a + ed13324 commit 6c1b1cc
Show file tree
Hide file tree
Showing 19 changed files with 561 additions and 218 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/target
notes.txt
.gitignore
pkg/
pkg/
web/node_modules
web/.next
14 changes: 14 additions & 0 deletions gg.ai
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

{
var b = 5;
print b;

{
var c = 10;
print c;
}

print c;
}

print b;
72 changes: 68 additions & 4 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{
scanner::{Lexer, TokenType},
vm::{self},
vm,
};
use std::fmt;

Expand All @@ -16,7 +16,10 @@ pub enum ASTNode {
Callee(String, Vec<ASTNode>), // function call with arguments
Let(String, Vec<ASTNode>),
Assign(String, Vec<ASTNode>),
If(Vec<ASTNode>, Vec<ASTNode>, Option<Vec<ASTNode>>), // condition, then, else
While(Vec<ASTNode>, Vec<ASTNode>), // condition, body
Print(Vec<ASTNode>),
Block(Vec<ASTNode>), // depth, statements
}

#[derive(Debug, Clone, Copy, PartialEq)]
Expand Down Expand Up @@ -77,7 +80,13 @@ impl<'a> Parser<'a> {
let statement = match self.lexer.peek().token_type {
TokenType::PRINT => self.parse_print(),
TokenType::LET => self.parse_let(),

TokenType::LeftBrace => {
self.lexer.next();
let statements = self.parse();
assert_eq!(self.lexer.next().token_type, TokenType::RightBrace);
ASTNode::Block(statements)
}
TokenType::RightBrace => break,
// contains equal pr +=, -=, *=, /=
TokenType::Identifier
if self.lexer.peek_n_type(2).contains(&TokenType::EQUAL)
Expand All @@ -96,6 +105,28 @@ impl<'a> Parser<'a> {
}
}
}
TokenType::IF => {
self.lexer.next();
assert_eq!(self.lexer.next().token_type, TokenType::LeftParen);
let condition = self.parse_expression();
assert_eq!(self.lexer.next().token_type, TokenType::RightParen);
let then_branch = self.parse_block();
let else_branch = if self.lexer.peek().token_type == TokenType::ELSE {
self.lexer.next();
Some(self.parse_block())
} else {
None
};
ASTNode::If(vec![condition], then_branch, else_branch)
}
TokenType::WHILE => {
self.lexer.next();
assert_eq!(self.lexer.next().token_type, TokenType::LeftParen);
let condition = self.parse_expression();
assert_eq!(self.lexer.next().token_type, TokenType::RightParen);
let body = self.parse_block();
ASTNode::While(vec![condition], body)
}
TokenType::SEMICOLON => {
self.lexer.next();
continue;
Expand Down Expand Up @@ -149,6 +180,13 @@ impl<'a> Parser<'a> {
Ok(ASTNode::Assign(identifier, vec![expr]))
}

fn parse_block(&mut self) -> Vec<ASTNode> {
assert_eq!(self.lexer.next().token_type, TokenType::LeftBrace);
let statements = self.parse();
assert_eq!(self.lexer.next().token_type, TokenType::RightBrace);
statements
}

fn parse_expression(&mut self) -> ASTNode {
expr_bp(self.lexer, 0)
}
Expand Down Expand Up @@ -344,9 +382,7 @@ fn infix_binding_power(op: Ops) -> Option<(u8, u8)> {
use colored::*;

impl fmt::Display for Ops {

fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

match self {
Ops::BinaryOp(BinaryOp::Add) => write!(f, "{}", "+".green()),
Ops::BinaryOp(BinaryOp::Sub) => write!(f, "{}", "-".green()),
Expand Down Expand Up @@ -395,9 +431,37 @@ impl fmt::Display for ASTNode {
ASTNode::Let(identifier, expr) => {
write!(f, "let {} = {}", identifier, expr[0])
}
ASTNode::Block(statements) => {
for stmt in statements {
write!(f, "{}", stmt)?;
}
write!(f, "")
}
ASTNode::Assign(identifier, expr) => {
write!(f, "{} = {}", identifier, expr[0])
}
ASTNode::If(condition, then_branch, else_branch) => {
write!(f, "if {} {{", condition[0])?;
for stmt in then_branch {
write!(f, "{}", stmt)?;
}
write!(f, "}}")?;
if !else_branch.is_none() {
write!(f, " else {{")?;
for stmt in else_branch {
write!(f, "{:?}", stmt)?;
}
write!(f, "}}")?;
}
write!(f, "")
}
ASTNode::While(condition, body) => {
write!(f, "while {} {{", condition[0])?;
for stmt in body {
write!(f, "{}", stmt)?;
}
write!(f, "}}")
}
ASTNode::Op(head, rest) => {
write!(f, "({}", head)?;
for s in rest {
Expand Down
27 changes: 23 additions & 4 deletions src/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::value::ValueType;
use crate::{tensor::Tensor, value::ValueType};

#[derive(Debug, Clone, Copy)]
#[repr(u8)]
Expand All @@ -12,6 +12,8 @@ pub enum OpCode {
OpSubtract,
OpMultiply,
OpDivide,
OpPower,

OpNot,
OpEqualEqual,
OpGreater,
Expand All @@ -22,7 +24,14 @@ pub enum OpCode {
OpDefineGlobal,
OpGetGlobal,
OpSetGlobal,
OpPower,

OpDefineLocal,
OpGetLocal,
OpSetLocal,

OpJumpIfFalse,
OpJump,
OpLoop,

OpCall,
}
Expand Down Expand Up @@ -71,19 +80,29 @@ impl std::fmt::Display for OpCode {
OpCode::OpSubtract => write!(f, "OP_SUBTRACT"),
OpCode::OpMultiply => write!(f, "OP_MULTIPLY"),
OpCode::OpDivide => write!(f, "OP_DIVIDE"),
OpCode::OpPower => write!(f, "OP_POWER"),

OpCode::OpNil => write!(f, "OP_NIL"),
OpCode::OpTrue => write!(f, "OP_TRUE"),
OpCode::OpFalse => write!(f, "OP_FALSE"),
OpCode::OpNot => write!(f, "OP_NOT"),
OpCode::OpEqualEqual => write!(f, "OP_EQUAL"),
OpCode::OpEqualEqual => write!(f, "OP_EQUAL_EQUAL"),
OpCode::OpGreater => write!(f, "OP_GREATER"),
OpCode::OpLess => write!(f, "OP_LESS"),
OpCode::OpPrint => write!(f, "OP_PRINT"),
OpCode::OpPop => write!(f, "OP_POP"),
OpCode::OpDefineGlobal => write!(f, "OP_DEFINE_GLOBAL"),
OpCode::OpGetGlobal => write!(f, "OP_GET_GLOBAL"),
OpCode::OpSetGlobal => write!(f, "OP_SET_GLOBAL"),
OpCode::OpPower => write!(f, "OP_POWER"),

OpCode::OpDefineLocal => write!(f, "OP_DEFINE_LOCAL"),
OpCode::OpGetLocal => write!(f, "OP_GET_LOCAL"),
OpCode::OpSetLocal => write!(f, "OP_SET_LOCAL"),

OpCode::OpJumpIfFalse => write!(f, "OP_JUMP_IF_FALSE"),
OpCode::OpJump => write!(f, "OP_JUMP"),
OpCode::OpLoop => write!(f, "OP_LOOP"),

OpCode::OpCall => write!(f, "OP_CALL"),
}
}
Expand Down
Loading

0 comments on commit 6c1b1cc

Please sign in to comment.