Skip to content

Commit

Permalink
stuff i guess
Browse files Browse the repository at this point in the history
  • Loading branch information
Somfic committed Oct 22, 2024
1 parent 2b8bca1 commit e515147
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 71 deletions.
5 changes: 1 addition & 4 deletions src/diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::scanner::lexeme::Token;
use std::collections::HashSet;

use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};

use crate::{files::Files, scanner::lexeme::Token};

pub struct PassResult<'a, T> {
pub result: T,
pub diagnostics: HashSet<Diagnostic<'a>>,
Expand Down
13 changes: 3 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
use anyhow::{Ok, Result};
use codespan_reporting::term::{
self,
termcolor::{ColorChoice, StandardStream},
};

use diagnostic::{Diagnostic, Snippet};
use files::Files;
use std::{collections::HashSet, env::args, fs::read};
use std::{env::args, fs::read};
use transpiler::{javascript::JavaScriptTranspiler, Transpiler};

pub mod diagnostic;
pub mod files;
pub mod parser;
pub mod scanner;
pub mod transpiler;
pub mod typechecker;

fn main() -> Result<()> {
let args: Vec<String> = args().skip(1).collect();
Expand Down Expand Up @@ -55,9 +50,7 @@ fn main() -> Result<()> {
let mut parser = parser::Parser::new(&scanner_pass.result);
let parser_pass = parser.parse().unwrap();

parser.print_diagnostics(&files);

let mut transpiled = JavaScriptTranspiler::transpile(&parser_pass);
let transpiled = JavaScriptTranspiler::transpile(&parser_pass);

println!("{}", transpiled);

Expand Down
76 changes: 59 additions & 17 deletions src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,55 @@ use std::{
hash::Hash,
};

use crate::scanner::lexeme::Token;
use crate::diagnostic::Range;

#[derive(Debug, Clone, PartialEq)]
pub enum Symbol {
Expression(Expression),
Statement(Statement),
Type(Type),
pub enum Symbol<'a> {
Expression(ExpressionSymbol<'a>),
Statement(StatementSymbol<'a>),
Type(TypeSymbol<'a>),
}

impl Symbol<'_> {
pub fn range(&self) -> &Range {
match self {
Symbol::Expression(expression) => &expression.range,
Symbol::Statement(statement) => &statement.range,
Symbol::Type(typest) => &typest.range,
}
}
}

pub struct ExpressionSymbol<'a> {
pub range: Range<'a>,
pub value: Expression,
}

impl<'a> ExpressionSymbol<'a> {
pub fn new(value: Expression, range: Range<'a>) -> Self {
Self { value, range }
}
}

pub struct StatementSymbol<'a> {
pub range: Range<'a>,
pub value: Satement,
}

impl<'a> StatementSymbol<'a> {
pub fn new(value: Satement, range: Range<'a>) -> Self {
Self { value, range }
}
}

pub struct TypeSymbol<'a> {
pub range: Range<'a>,
pub value: Type,
}

impl<'a> TypeSymbol<'a> {
pub fn new(value: Type, range: Range<'a>) -> Self {
Self { value, range }
}
}

#[derive(Debug, Clone, PartialEq)]
Expand All @@ -27,8 +69,8 @@ pub enum Expression {
}

#[derive(Debug, Clone, PartialEq)]
pub enum Statement {
Block(Vec<Statement>),
pub enum Satement {
Block(Vec<Expression>),
Declaration(String, Option<Type>, Expression),
Expression(Expression),
Struct(String, HashSet<FieldSignature>),
Expand All @@ -39,10 +81,18 @@ pub enum Statement {
Implementation(String, String, HashSet<Function>),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Type {
Void,
Symbol(String),
Array(Box<Type>),
Tuple(HashMap<String, Type>),
}

#[derive(Debug, Clone)]
pub struct Function {
pub signature: FunctionSignature,
pub body: Box<Statement>,
pub body: Box<Satement>,
}

impl PartialEq for Function {
Expand Down Expand Up @@ -109,11 +159,3 @@ pub enum BinaryOperation {
Times,
Divide,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Type {
Void,
Symbol(String),
Array(Box<Type>),
Tuple(HashMap<String, Type>),
}
4 changes: 1 addition & 3 deletions src/parser/expression/functions.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::collections::HashMap;

use crate::{
parser::{
ast::Expression,
lookup::{BindingPower, Lookup},
macros::{expect_token, expect_value, optional_token},
macros::{expect_token, optional_token},
},
scanner::lexeme::TokenType,
};
Expand Down
2 changes: 1 addition & 1 deletion src/parser/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub enum BindingPower {

pub type TypeHandler<'a> = fn(&mut Parser<'a>) -> ParseResult<'a, Type>;
pub type LeftTypeHandler<'a> = fn(&mut Parser<'a>, Type, BindingPower) -> ParseResult<'a, Type>;
pub type StatementHandler<'a> = fn(&mut Parser<'a>) -> ParseResult<'a, Statement>;
pub type StatementHandler<'a> = fn(&mut Parser<'a>) -> ParseResult<'a, Satement>;
pub type ExpressionHandler<'a> = fn(&mut Parser<'a>) -> ParseResult<'a, Expression>;
pub type LeftExpressionHandler<'a> =
fn(&mut Parser<'a>, Expression, BindingPower) -> ParseResult<'a, Expression>;
Expand Down
10 changes: 7 additions & 3 deletions src/parser/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ macro_rules! expect_token {
format!("Expected {} here", TokenType::$token),
))
.with_note(format!(
"Expected {}, but got {} instead",
"Expected {}, but found {} instead",
TokenType::$token,
token.token_type
)),
Expand Down Expand Up @@ -88,8 +88,12 @@ macro_rules! either_token {
"Unexpected token",
))
.with_note(format!(
"Expected one of {:?}, but got {} instead",
vec![ $(TokenType::$token),* ],
"Expected {}, but found {} instead",
vec![ $(TokenType::$token),* ]
.iter()
.map(|t| format!("{}", t))
.collect::<Vec<String>>()
.join(" or "),
token.token_type
)),
)
Expand Down
16 changes: 4 additions & 12 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use crate::{
diagnostic::{Diagnostic, PassResult},
files::Files,
scanner::lexeme::{Token, TokenType, TokenValue},
};
use ast::{Statement, Symbol};
use crate::{diagnostic::Diagnostic, files::Files, scanner::lexeme::Token};
use ast::{Satement, StatementSymbol, Symbol};
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};
use lookup::Lookup;
use std::collections::HashSet;
Expand Down Expand Up @@ -34,7 +30,7 @@ impl<'a> Parser<'a> {
}
}

pub fn parse(&mut self) -> ParseResult<'a, Symbol> {
pub fn parse(&mut self) -> ParseResult<'a, StatementSymbol> {
let mut statements = Vec::new();
let mut panic_mode = false;

Expand All @@ -58,7 +54,7 @@ impl<'a> Parser<'a> {
}
}

Ok(Symbol::Statement(Statement::Block(statements)))
Ok(StatementSymbol::new(Satement::Block(statements)))
}

pub(crate) fn peek(&self) -> Option<&Token<'a>> {
Expand All @@ -70,10 +66,6 @@ impl<'a> Parser<'a> {
self.tokens.get(self.cursor - 1)
}

pub(crate) fn peek_next(&self) -> Option<&Token<'a>> {
self.tokens.get(self.cursor + 1)
}

pub(crate) fn has_tokens(&self) -> bool {
self.cursor < self.tokens.len()
}
Expand Down
9 changes: 4 additions & 5 deletions src/parser/statement/enums.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::collections::{HashMap, HashSet};

use crate::{
parser::{
ast::{EnumMember, Statement},
lookup::{BindingPower, Lookup},
macros::{expect_token, expect_value, optional_token, warn_unneeded_token},
typest, ParseResult, Parser,
lookup::Lookup,
macros::{expect_token, expect_value, optional_token},
ParseResult, Parser,
},
scanner::lexeme::TokenType,
};
use std::collections::HashSet;

use super::variables;

Expand Down
9 changes: 4 additions & 5 deletions src/parser/statement/structs.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use super::variables;
use crate::{
parser::{
ast::{FieldSignature, Statement},
lookup::{BindingPower, Lookup},
lookup::Lookup,
macros::{expect_token, expect_value, optional_token},
typest, ParseResult, Parser,
ParseResult, Parser,
},
scanner::lexeme::TokenType,
};
use std::collections::{HashMap, HashSet};

use super::variables;
use std::collections::HashSet;

pub fn register(lookup: &mut Lookup) {
lookup.add_statement_handler(TokenType::Struct, parse_struct);
Expand Down
4 changes: 2 additions & 2 deletions src/parser/statement/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::collections::HashSet;

use crate::{
parser::{
ast::{FieldSignature, Function, Statement},
lookup::{BindingPower, Lookup},
ast::{Function, Statement},
lookup::Lookup,
macros::{either_token, expect_token, expect_value},
ParseResult, Parser,
},
Expand Down
2 changes: 1 addition & 1 deletion src/parser/typest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{

use super::{
ast::Type,
lookup::{self, BindingPower, Lookup},
lookup::{BindingPower, Lookup},
macros::{expect_token, expect_value},
ParseResult, Parser,
};
Expand Down
4 changes: 2 additions & 2 deletions src/transpiler/javascript/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ fn transpile_struct_initializer(_name: &str, fields: &HashMap<String, Expression
format!("{{ {} }}", fields)
}

fn transpile_function_call(name: &str, parameters: &Vec<Expression>) -> String {
fn transpile_function_call(name: &str, parameters: &[Expression]) -> String {
let parameters = parameters
.iter()
.map(|parameter| transpile(parameter))
.map(transpile)
.collect::<Vec<String>>()
.join(", ");

Expand Down
3 changes: 1 addition & 2 deletions src/transpiler/javascript/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::Transpiler;
use crate::parser::ast::Symbol;
use anyhow::Result;
use deno_core::{JsRuntime, RuntimeOptions};
use deno_core::JsRuntime;

pub mod expressions;
pub mod statements;
Expand Down
8 changes: 4 additions & 4 deletions src/transpiler/javascript/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ pub fn transpile(statement: &Statement) -> String {
}
}

fn transpile_block(block: &Vec<Statement>) -> String {
fn transpile_block(block: &[Statement]) -> String {
block
.iter()
.map(|statement| transpile(statement))
.map(transpile)
.collect::<Vec<String>>()
.join("\n")
}
Expand All @@ -43,11 +43,11 @@ fn transpile_expression(expression: &Expression) -> String {
format!("{};", expression)
}

fn transpile_struct(name: &String, fields: &HashSet<FieldSignature>) -> String {
fn transpile_struct(name: &str, fields: &HashSet<FieldSignature>) -> String {
"".to_string()
}

fn transpile_enum(name: &String, members: &HashSet<EnumMember>) -> String {
fn transpile_enum(name: &str, members: &HashSet<EnumMember>) -> String {
"".to_string()
}

Expand Down
7 changes: 7 additions & 0 deletions src/typechecker/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use std::collections::HashSet;

use crate::{diagnostic::Diagnostic, parser::ast::Symbol};

pub fn check(ast: &Symbol) -> Result<(), HashSet<Diagnostic>> {
Ok(())
}

0 comments on commit e515147

Please sign in to comment.