Compare commits
No commits in common. "02f3c9aee622a4e2cccf106aa90a0b048efbbe86" and "61de3b100f3c6a22d87accfaa8d76b36ddfaeb59" have entirely different histories.
02f3c9aee6
...
61de3b100f
@ -13,11 +13,11 @@ impl Transpilable for Expression {
|
||||
match self {
|
||||
Expression::Number(value) => format!("{}", value),
|
||||
Expression::String(value) => {
|
||||
format!("{}", *value)
|
||||
format!("\"{}\"", *value)
|
||||
}
|
||||
Expression::Boolean(value) => String::from(if *value { "true" } else { "false" }),
|
||||
Expression::Identifier(value) => format!("{}", *value),
|
||||
Expression::FunctionCall(f) => f.transpile(),
|
||||
Expression::FunctionCall(_) => todo!("FunctionCall codegen is not implemented yet"),
|
||||
Expression::BinaryOperator(_, _, _) => {
|
||||
todo!("BinaryOperator codegen is not implemented yet")
|
||||
}
|
||||
@ -44,7 +44,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn should_transpile_string() {
|
||||
let str = String::from("\"Hello world\"");
|
||||
let str = String::from("Hello world");
|
||||
let exp = Expression::String(Box::new(str));
|
||||
let result = exp.transpile();
|
||||
|
||||
|
@ -1,9 +0,0 @@
|
||||
use crate::syntax::ast::functions::FunctionCall;
|
||||
|
||||
use super::Transpilable;
|
||||
|
||||
impl Transpilable for FunctionCall {
|
||||
fn transpile(&self) -> String {
|
||||
format!("{}();", self.function.transpile())
|
||||
}
|
||||
}
|
@ -32,7 +32,7 @@ mod tests {
|
||||
TopLevelDeclaration::FunctionDeclaration(fun_decl) => {
|
||||
let transpiled = fun_decl.transpile();
|
||||
|
||||
assert_eq!("function id() {\n\n}", transpiled);
|
||||
assert_eq!("function id() {}", transpiled);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ use crate::syntax::ast::ModuleAST;
|
||||
mod binding;
|
||||
mod block;
|
||||
mod expression;
|
||||
mod function_call;
|
||||
mod function_declaration;
|
||||
mod module_ast;
|
||||
mod statement;
|
||||
|
@ -37,6 +37,6 @@ mod tests {
|
||||
|
||||
let result = module.transpile();
|
||||
|
||||
assert_eq!("<?php\n\n$identifier = 322;\n", result);
|
||||
assert_eq!("$identifier = 322;", result);
|
||||
}
|
||||
}
|
||||
|
@ -4,9 +4,6 @@ use super::Transpilable;
|
||||
|
||||
impl Transpilable for Statement {
|
||||
fn transpile(&self) -> String {
|
||||
match self {
|
||||
Statement::Binding(binding) => binding.transpile(),
|
||||
Statement::FunctionCall(function_call) => function_call.transpile(),
|
||||
}
|
||||
String::from("// TODO (statement)")
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ pub fn scan(chars: &Vec<char>, start_pos: usize) -> LexResult {
|
||||
Some(c) if *c == ' ' => match look_ahead_for_new_line(chars, start_pos + 1) {
|
||||
Some(next_pos) => scan(chars, next_pos),
|
||||
None => {
|
||||
let token = Token::new(String::from(""), start_pos, TokenType::NewLine);
|
||||
let token = Token::new(String::from(";"), start_pos, TokenType::NewLine);
|
||||
LexResult::Some(token, start_pos)
|
||||
}
|
||||
},
|
||||
|
@ -1,8 +1,6 @@
|
||||
use super::Expression;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FunctionCall {
|
||||
pub function: Box<Expression>,
|
||||
pub identifier: Box<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -104,19 +104,4 @@ mod tests {
|
||||
|
||||
assert_eq!(block.statements.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_block_3() {
|
||||
let tokens = get_tokens(&String::from("{\n f()\n}")).unwrap();
|
||||
let block = parse_block(&tokens, 0);
|
||||
|
||||
let block = match block {
|
||||
ParseResult::Ok(p, _) => p,
|
||||
_ => {
|
||||
panic!("Expected a block, got: {:?}\n\n{:?}", block, tokens)
|
||||
},
|
||||
};
|
||||
|
||||
assert_eq!(block.statements.len(), 1);
|
||||
}
|
||||
}
|
||||
|
@ -1,42 +0,0 @@
|
||||
use crate::{
|
||||
lexic::token::Token,
|
||||
syntax::{
|
||||
ast::{functions::FunctionCall, Expression},
|
||||
functions::arguments_list,
|
||||
ParseResult,
|
||||
},
|
||||
};
|
||||
|
||||
/// Parses a function call expression.
|
||||
///
|
||||
/// ```ebnf
|
||||
/// function call expr = primary, "(", (arguments list)?, ")"
|
||||
/// | primary;
|
||||
/// ```
|
||||
pub fn try_parse(tokens: &Vec<Token>, pos: usize) -> ParseResult<Expression, ()> {
|
||||
let (primary_expr, next_pos) = match super::primary::try_parse(tokens, pos) {
|
||||
ParseResult::Ok(expr, next_pos) => (expr, next_pos),
|
||||
_ => return ParseResult::Unmatched,
|
||||
};
|
||||
|
||||
// Parse arguments list
|
||||
let (_args_list, next_pos) = match arguments_list::try_parse(tokens, next_pos) {
|
||||
ParseResult::Ok(args, next) => (args, next),
|
||||
ParseResult::Err(err) => return ParseResult::Err(err),
|
||||
_ => {
|
||||
return ParseResult::Ok(primary_expr, next_pos);
|
||||
}
|
||||
};
|
||||
|
||||
let fun_call = FunctionCall {
|
||||
function: Box::new(primary_expr),
|
||||
};
|
||||
|
||||
ParseResult::Ok(Expression::FunctionCall(fun_call), next_pos)
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
|
||||
}
|
@ -4,7 +4,6 @@ use crate::lexic::token::Token;
|
||||
mod comparison;
|
||||
mod equality;
|
||||
mod factor;
|
||||
pub mod function_call_expr;
|
||||
mod primary;
|
||||
mod term;
|
||||
mod unary;
|
||||
|
@ -2,7 +2,6 @@ use crate::{
|
||||
lexic::token::{Token, TokenType},
|
||||
syntax::{ast::Expression, ParseResult},
|
||||
};
|
||||
use super::super::utils::Tokenizer;
|
||||
|
||||
/// This grammar may not be up to date. Refer to the spec for the latest grammar.
|
||||
///
|
||||
@ -10,22 +9,32 @@ use super::super::utils::Tokenizer;
|
||||
/// primary = number | string | boolean | identifier | ("(", expression, ")");
|
||||
/// ```
|
||||
pub fn try_parse(tokens: &Vec<Token>, pos: usize) -> ParseResult<Expression, ()> {
|
||||
match tokens.get_significant(pos) {
|
||||
Some((token, token_pos)) => match token.token_type {
|
||||
/*
|
||||
TODO: Incorporate function_call into the grammar, figure out its precedence.
|
||||
match function_call::try_parse(tokens, pos) {
|
||||
super::ParseResult::Ok(function_call, next_pos) => {
|
||||
return ParseResult::Ok::<_, ()>(Expression::FunctionCall(function_call), next_pos)
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
*/
|
||||
|
||||
match tokens.get(pos) {
|
||||
Some(token) => match token.token_type {
|
||||
TokenType::Number => {
|
||||
ParseResult::Ok(Expression::Number(Box::new(token.value.clone())), token_pos + 1)
|
||||
ParseResult::Ok(Expression::Number(Box::new(token.value.clone())), pos + 1)
|
||||
}
|
||||
TokenType::String => {
|
||||
ParseResult::Ok(Expression::String(Box::new(token.value.clone())), token_pos + 1)
|
||||
ParseResult::Ok(Expression::String(Box::new(token.value.clone())), pos + 1)
|
||||
}
|
||||
TokenType::Identifier if token.value == "true" || token.value == "false" => {
|
||||
ParseResult::Ok(Expression::Boolean(token.value == "true"), token_pos + 1)
|
||||
ParseResult::Ok(Expression::Boolean(token.value == "true"), pos + 1)
|
||||
}
|
||||
TokenType::Identifier => ParseResult::Ok(
|
||||
Expression::Identifier(Box::new(token.value.clone())),
|
||||
token_pos + 1,
|
||||
pos + 1,
|
||||
),
|
||||
TokenType::LeftParen => parse_parenthesized_expression(tokens, token_pos),
|
||||
TokenType::LeftParen => parse_parenthesized_expression(tokens, pos),
|
||||
_ => ParseResult::Unmatched,
|
||||
},
|
||||
None => ParseResult::Unmatched,
|
||||
|
@ -1,15 +1,13 @@
|
||||
use crate::{
|
||||
lexic::token::Token,
|
||||
syntax::{ast::Expression, ParseResult},
|
||||
syntax::{ast::Expression, expression::primary, ParseResult},
|
||||
};
|
||||
|
||||
use super::function_call_expr;
|
||||
|
||||
/// Parses an unary expression.
|
||||
///
|
||||
/// ```ebnf
|
||||
/// unary = ("!" | "-"), expression
|
||||
/// | function call expr;
|
||||
/// | primary;
|
||||
/// ```
|
||||
pub fn try_parse(tokens: &Vec<Token>, pos: usize) -> ParseResult<Expression, ()> {
|
||||
match tokens.get(pos) {
|
||||
@ -22,6 +20,6 @@ pub fn try_parse(tokens: &Vec<Token>, pos: usize) -> ParseResult<Expression, ()>
|
||||
_ => ParseResult::Unmatched,
|
||||
}
|
||||
}
|
||||
_ => function_call_expr::try_parse(tokens, pos),
|
||||
_ => primary::try_parse(tokens, pos),
|
||||
}
|
||||
}
|
||||
|
95
src/syntax/functions/function_call.rs
Normal file
95
src/syntax/functions/function_call.rs
Normal file
@ -0,0 +1,95 @@
|
||||
use crate::{
|
||||
lexic::token::{Token, TokenType},
|
||||
syntax::{ast::functions::FunctionCall, utils::parse_token_type, ParseResult},
|
||||
};
|
||||
|
||||
pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> ParseResult<FunctionCall, ()> {
|
||||
let mut current_pos = pos;
|
||||
|
||||
// TODO: Use an expression instead of a fixed identifier
|
||||
// Parse identifier
|
||||
let (identifier, next_pos) = match parse_token_type(tokens, current_pos, TokenType::Identifier)
|
||||
{
|
||||
ParseResult::Ok(id, next) => (id, next),
|
||||
ParseResult::Err(err) => return ParseResult::Err(err),
|
||||
ParseResult::Mismatch(_) => {
|
||||
return ParseResult::Unmatched;
|
||||
}
|
||||
ParseResult::Unmatched => {
|
||||
return ParseResult::Unmatched;
|
||||
}
|
||||
};
|
||||
current_pos = next_pos;
|
||||
|
||||
// Parse arguments list
|
||||
let (_args_list, next_pos) = match super::arguments_list::try_parse(tokens, current_pos) {
|
||||
ParseResult::Ok(args, next) => (args, next),
|
||||
ParseResult::Err(err) => return ParseResult::Err(err),
|
||||
ParseResult::Mismatch(_) => {
|
||||
return ParseResult::Unmatched;
|
||||
}
|
||||
ParseResult::Unmatched => {
|
||||
return ParseResult::Unmatched;
|
||||
}
|
||||
};
|
||||
current_pos = next_pos;
|
||||
|
||||
ParseResult::Ok(
|
||||
FunctionCall {
|
||||
identifier: Box::new(identifier.value.clone()),
|
||||
},
|
||||
current_pos,
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::lexic::get_tokens;
|
||||
|
||||
#[test]
|
||||
fn should_not_parse_identifier_alone() {
|
||||
let tokens = get_tokens(&String::from("function_name")).unwrap();
|
||||
let fun_decl = try_parse(&tokens, 0);
|
||||
|
||||
let ParseResult::Unmatched = fun_decl else {
|
||||
panic!("Expected an unmatched result: {:?}", fun_decl);
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_parse_minimal_construct() {
|
||||
let tokens = get_tokens(&String::from("function_name()")).unwrap();
|
||||
let fun_decl = try_parse(&tokens, 0);
|
||||
|
||||
let ParseResult::Ok(_call, next) = fun_decl else {
|
||||
panic!("Expected a result, got: {:?}", fun_decl);
|
||||
};
|
||||
|
||||
assert_eq!(next, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_parse_minimal_construct_2() {
|
||||
let tokens = get_tokens(&String::from("function_name ( )")).unwrap();
|
||||
let fun_decl = try_parse(&tokens, 0);
|
||||
|
||||
let ParseResult::Ok(_call, next) = fun_decl else {
|
||||
panic!("Expected a result, got: {:?}", fun_decl);
|
||||
};
|
||||
|
||||
assert_eq!(next, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_parse_minimal_construct_3() {
|
||||
let tokens = get_tokens(&String::from("function_name\n(\n \n)")).unwrap();
|
||||
let fun_decl = try_parse(&tokens, 0);
|
||||
|
||||
let ParseResult::Ok(_call, next) = fun_decl else {
|
||||
panic!("Expected a result, got: {:?}", fun_decl);
|
||||
};
|
||||
|
||||
assert_eq!(next, 5);
|
||||
}
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
pub mod arguments_list;
|
||||
|
||||
pub mod function_call;
|
||||
pub mod function_declaration;
|
||||
|
@ -85,14 +85,11 @@ factor = unary, (("/" | "*"), unary)*;
|
||||
unary = ("!" | "-"), expression
|
||||
| primary;
|
||||
|
||||
function call expr = primary, (arguments list)?
|
||||
| primary;
|
||||
function call = primary, (arguments list)?;
|
||||
|
||||
primary = number | string | boolean | identifier | ("(", expression, ")");
|
||||
```
|
||||
|
||||
```thp
|
||||
primary()
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
@ -1,11 +1,6 @@
|
||||
use crate::lexic::token::Token;
|
||||
|
||||
use super::{
|
||||
ast::{statement::Statement, Expression},
|
||||
binding,
|
||||
expression::function_call_expr,
|
||||
ParseResult,
|
||||
};
|
||||
use super::{ast::statement::Statement, binding, functions::function_call, ParseResult};
|
||||
|
||||
pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> ParseResult<Statement, ()> {
|
||||
None.or_else(|| match binding::try_parse(tokens, pos) {
|
||||
@ -13,13 +8,8 @@ pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> ParseResult<Statemen
|
||||
ParseResult::Err(err) => Some(ParseResult::Err(err)),
|
||||
_ => None,
|
||||
})
|
||||
.or_else(|| match function_call_expr::try_parse(tokens, pos) {
|
||||
ParseResult::Ok(f, next) => {
|
||||
let Expression::FunctionCall(f) = f else {
|
||||
return None;
|
||||
};
|
||||
Some(ParseResult::Ok(Statement::FunctionCall(f), next))
|
||||
}
|
||||
.or_else(|| match function_call::try_parse(tokens, pos) {
|
||||
ParseResult::Ok(f, next) => Some(ParseResult::Ok(Statement::FunctionCall(f), next)),
|
||||
ParseResult::Err(err) => Some(ParseResult::Err(err)),
|
||||
_ => None,
|
||||
})
|
||||
|
@ -5,34 +5,6 @@ use crate::{
|
||||
|
||||
use super::ParseResult;
|
||||
|
||||
pub trait Tokenizer {
|
||||
fn get_significant<'a>(&'a self, index: usize) -> Option<(&'a Token, usize)>;
|
||||
}
|
||||
|
||||
impl Tokenizer for Vec<Token> {
|
||||
/// Returns the first non whitespace token at index & the position the found token
|
||||
fn get_significant<'a>(&'a self, index: usize) -> Option<(&'a Token, usize)> {
|
||||
let mut current_pos = index;
|
||||
|
||||
// Ignore all whitespace and newlines
|
||||
loop {
|
||||
match self.get(current_pos) {
|
||||
Some(token) => {
|
||||
if token.token_type == TokenType::INDENT
|
||||
|| token.token_type == TokenType::DEDENT
|
||||
|| token.token_type == TokenType::NewLine
|
||||
{
|
||||
current_pos += 1;
|
||||
} else {
|
||||
return Some((token, current_pos));
|
||||
}
|
||||
}
|
||||
None => return None,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Expects the token at `pos` to be of type `token_type`
|
||||
pub fn try_token_type(tokens: &Vec<Token>, pos: usize, token_type: TokenType) -> Result3<&Token> {
|
||||
match tokens.get(pos) {
|
||||
|
Loading…
Reference in New Issue
Block a user