Skip to content

Commit

Permalink
fix generate executale that was giving seg fault and also refactored …
Browse files Browse the repository at this point in the history
…parser
  • Loading branch information
tahadostifam committed Feb 10, 2025
1 parent 8be5629 commit ef1ccbd
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 48 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ dump:
cargo run -- dump --dump-type=ir ./examples/main.cy ./tmp/main

run:
cargo run -- run ./examples/main.cy
cargo run -- run ./examples/main.cy

build:
cargo run -- build ./examples/main.cy ./tmp/program;
3 changes: 2 additions & 1 deletion compiler/src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ast::ast::*;
use ast::token::*;
use gccjit_sys::*;
use utils::compiler_error;
use utils::purify_string::purify_string;

use crate::scope::ScopeRef;
use crate::Compiler;
Expand Down Expand Up @@ -516,7 +517,7 @@ impl Compiler {
unsafe { gcc_jit_context_new_rvalue_from_int(self.context, Compiler::i8_type(self.context), value) }
}
Literal::String(string_literal) => unsafe {
let value = CString::new(self.purify_string(string_literal.raw)).unwrap();
let value = CString::new(purify_string(string_literal.raw)).unwrap();
gcc_jit_context_new_string_literal(self.context, value.as_ptr())
},
Literal::Char(char_literal) => unsafe {
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ impl Compiler {
unsafe { gcc_jit_context_add_driver_option(self.context, optname.as_ptr()) };
}

let optname = CString::new(format!("-lm")).unwrap();
// Explicit C_STDLIB link
let optname = CString::new(format!("-lc")).unwrap();
unsafe { gcc_jit_context_add_driver_option(self.context, optname.as_ptr()) };
}

Expand Down
12 changes: 8 additions & 4 deletions compiler/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use utils::compiler_error;
impl Compiler {
pub fn execute(&self) {
let result = unsafe { gcc_jit_context_compile(self.context) };

if result.is_null() {
exit(1);
}

let name = CString::new("main").unwrap();
let main = unsafe { gcc_jit_result_get_code(result, name.as_ptr()) };
if main.is_null() {
Expand Down Expand Up @@ -46,7 +46,7 @@ impl Compiler {
)
};
}

pub fn make_dynamic_library(&self, file_path: String) {
unsafe {
let file_path = CString::new(file_path).unwrap();
Expand All @@ -68,7 +68,11 @@ impl Compiler {
pub fn make_dump_asm(&self, file_path: String) {
unsafe {
let file_path = CString::new(file_path).unwrap();
gcc_jit_context_compile_to_file(self.context, gcc_jit_output_kind::GCC_JIT_OUTPUT_KIND_ASSEMBLER, file_path.as_ptr())
gcc_jit_context_compile_to_file(
self.context,
gcc_jit_output_kind::GCC_JIT_OUTPUT_KIND_ASSEMBLER,
file_path.as_ptr(),
)
};
}

Expand Down
11 changes: 0 additions & 11 deletions compiler/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,6 @@ impl Compiler {
}
}

pub fn purify_string(&self, str: String) -> String {
str.replace("\\n", "\n")
.replace("\\t", "\t")
.replace("\\r", "\r")
.replace("\\b", r"\b")
.replace("\\a", r"\a")
.replace("\\v", r"\v")
.replace("\\f", r"\f")
.replace("\\'", r"\'")
}

pub fn is_float_data_type(&mut self, type1: *mut gcc_jit_type) -> bool {
type1 == Compiler::f32_type(self.context) || type1 == Compiler::f64_type(self.context)
}
Expand Down
4 changes: 2 additions & 2 deletions examples/main.cy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import io;

pub fn main() {
printf("Hello: %s %s\n", "adad", "aasd");
}
IO.printf("Hi %s.\n", "Taha");
}
32 changes: 19 additions & 13 deletions parser/src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,24 @@ impl<'a> Parser<'a> {
}
TokenKind::LeftParen => {
self.next_token(); // consume the identifier token
Some(self.parse_func_call(left, left_start))

let expr = self.parse_func_call(left, left_start);

// ANCHOR
// if !(self.current_token_is(TokenKind::RightParen) || self.current_token_is(TokenKind::Comma))
// && self.current_token_is(TokenKind::Semicolon)
// {
// return Some(Err(CompileTimeError {
// location: self.current_location(),
// etype: ParserErrorType::MissingSemicolon,
// file_name: Some(self.lexer.file_name.clone()),
// code_raw: Some(self.lexer.select(left_start..self.current_token.span.end)),
// verbose: None,
// caret: true,
// }));
// }

Some(expr)
}
_ => None,
}
Expand Down Expand Up @@ -355,17 +372,6 @@ impl<'a> Parser<'a> {
Expression::Identifier(identifier) => {
self.next_token();

if !self.current_token_is(TokenKind::Semicolon) {
return Err(CompileTimeError {
location: self.current_location(),
etype: ParserErrorType::MissingSemicolon,
file_name: Some(self.lexer.file_name.clone()),
code_raw: Some(self.lexer.select(start..self.current_token.span.end)),
verbose: None,
caret: true,
});
}

Ok(Expression::FuncCall(FuncCall {
func_name: identifier,
arguments: arguments.0,
Expand Down Expand Up @@ -513,7 +519,7 @@ impl<'a> Parser<'a> {
loc: self.current_location(),
}),
});

match self.current_token.kind {
TokenKind::Dot => {
self.next_token(); // consume field_name
Expand Down
28 changes: 17 additions & 11 deletions parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,8 @@ pub fn parse_program(file_path: String) -> (Program, String) {
}
}
Err(errors) => {
for error in errors {
println!("{}", error);
}

std::process::exit(1);
parser.display_parser_errors(errors);
panic!();
}
};

Expand Down Expand Up @@ -100,6 +97,13 @@ impl<'a> Parser<'a> {
}
}

pub fn display_parser_errors(&mut self, errors: Vec<CompileTimeError<ParserErrorType>>) {
if errors.len() > 0 {
println!("{}", errors[0]);
std::process::exit(1);
}
}

/// Finalizes the program parse by checking for errors.
pub fn finalize_program_parse(&self, program: Program) -> Result<Program, Vec<ParseError>> {
if self.errors.is_empty() {
Expand Down Expand Up @@ -144,9 +148,10 @@ impl<'a> Parser<'a> {
location: self.current_location(),
etype: ParserErrorType::UnexpectedToken(token_kind, self.current_token.kind.clone()),
file_name: Some(self.lexer.file_name.clone()),
code_raw: Some(self
.lexer
.select(self.current_token.span.start..self.current_token.span.end)),
code_raw: Some(
self.lexer
.select(self.current_token.span.start..self.current_token.span.end),
),
verbose: None,
caret: true,
})
Expand All @@ -164,9 +169,10 @@ impl<'a> Parser<'a> {
location: self.current_location(),
etype: ParserErrorType::UnexpectedToken(self.current_token.kind.clone(), token_kind),
file_name: Some(self.lexer.file_name.clone()),
code_raw: Some(self
.lexer
.select(self.current_token.span.start..self.current_token.span.end)),
code_raw: Some(
self.lexer
.select(self.current_token.span.start..self.current_token.span.end),
),
verbose: None,
caret: true,
})
Expand Down
6 changes: 4 additions & 2 deletions stdlib/io.cy
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
extern fn printf(fmt: string, ...): void as _printf;

pub fn printf(fmt: string, ...) {
_printf(fmt);
pub struct IO {
pub fn printf(fmt: string, ...) {
_printf(fmt);
}
}
2 changes: 1 addition & 1 deletion utils/src/compile_time_errors/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl<ErrorType: CompileTypeErrorType> fmt::Display for CompileTimeError<ErrorTyp
let code_raw = code_raw.split("\n");

for (idx, line) in code_raw.clone().into_iter().enumerate() {
write!(f, "\t{}", line)?;
write!(f, "{}", line)?;

if idx == code_raw.clone().count() {
write!(f, "\n")?;
Expand Down
3 changes: 2 additions & 1 deletion utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod debugging_logs;
pub mod fs;
pub mod compile_time_errors;
pub mod generate_random_hex;
pub mod generate_random_hex;
pub mod purify_string;
10 changes: 10 additions & 0 deletions utils/src/purify_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pub fn purify_string(str: String) -> String {
str.replace("\\n", "\n")
.replace("\\t", "\t")
.replace("\\r", "\r")
.replace("\\b", r"\b")
.replace("\\a", r"\a")
.replace("\\v", r"\v")
.replace("\\f", r"\f")
.replace("\\'", r"\'")
}

0 comments on commit ef1ccbd

Please sign in to comment.