Skip to content

Commit

Permalink
Made 32-bit ints assumption the default, and made it so string litera…
Browse files Browse the repository at this point in the history
…ls can be used inside of pragma directives instead of cstring literals
  • Loading branch information
IsaacShelton committed Sep 22, 2024
1 parent 404a700 commit f56eba3
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 24 deletions.
18 changes: 12 additions & 6 deletions src/ast/workspace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ impl<'a> AstWorkspace<'a> {
let settings = AppendOnlyVec::new();

assert_eq!(
settings.push(Settings {
adept_version: AdeptVersion::CURRENT,
debug_skip_merging_helper_exprs: false,
imported_namespaces: vec![],
assume_int_at_least_32_bits: false,
}),
settings.push(Settings::default()),
Self::DEFAULT_SETTINGS_ID.0
);

Expand Down Expand Up @@ -120,6 +115,17 @@ pub struct Settings {
pub assume_int_at_least_32_bits: bool,
}

impl Default for Settings {
fn default() -> Settings {
Settings {
adept_version: AdeptVersion::CURRENT,
debug_skip_merging_helper_exprs: false,
imported_namespaces: vec![],
assume_int_at_least_32_bits: true,
}
}
}

impl Settings {
pub fn c_integer_assumptions(&self) -> CIntegerAssumptions {
CIntegerAssumptions {
Expand Down
20 changes: 17 additions & 3 deletions src/interpreter/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub enum ProjectKind {
WindowedApp = 1,
}

#[derive(Debug, Default)]
#[derive(Debug)]
pub struct BuildSystemSyscallHandler {
pub projects: Vec<Project>,
pub version: Option<AdeptVersion>,
Expand All @@ -39,6 +39,20 @@ pub struct BuildSystemSyscallHandler {
pub assume_int_at_least_32_bits: bool,
}

impl Default for BuildSystemSyscallHandler {
fn default() -> Self {
Self {
projects: vec![],
version: None,
link_filenames: HashSet::new(),
link_frameworks: HashSet::new(),
debug_skip_merging_helper_exprs: false,
imported_namespaces: vec![],
assume_int_at_least_32_bits: true,
}
}
}

fn read_cstring(memory: &Memory, value: &Value) -> String {
let mut string = String::new();
let mut address = value.as_u64().unwrap();
Expand Down Expand Up @@ -125,9 +139,9 @@ impl SyscallHandler for BuildSystemSyscallHandler {
.push(read_cstring(memory, &args[0]).into_boxed_str());
Value::Literal(ir::Literal::Void)
}
ir::InterpreterSyscallKind::AssumeIntAtLeast32Bits => {
ir::InterpreterSyscallKind::DontAssumeIntAtLeast32Bits => {
assert_eq!(args.len(), 0);
self.assume_int_at_least_32_bits = true;
self.assume_int_at_least_32_bits = false;
Value::Literal(ir::Literal::Void)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/interpreter_env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
));

file.functions.push(thin_void_function(
"assumeIntAtLeast32Bits",
InterpreterSyscallKind::AssumeIntAtLeast32Bits,
"dontAssumeIntAtLeast32Bits",
InterpreterSyscallKind::DontAssumeIntAtLeast32Bits,
));

file.functions.push(Function {
Expand Down
2 changes: 1 addition & 1 deletion src/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub enum InterpreterSyscallKind {
BuildLinkFrameworkName,
Experimental,
ImportNamespace,
AssumeIntAtLeast32Bits,
DontAssumeIntAtLeast32Bits,
}

#[derive(Clone, Debug)]
Expand Down
13 changes: 12 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,22 @@ pub fn parse(

pub struct Parser<'a, I: Inflow<Token>> {
pub input: Input<'a, I>,
pub treat_string_literals_as_cstring_literals: bool,
}

impl<'a, I: Inflow<Token>> Parser<'a, I> {
pub fn new(input: Input<'a, I>) -> Self {
Self { input }
Self {
input,
treat_string_literals_as_cstring_literals: false,
}
}

pub fn new_for_pragma(input: Input<'a, I>) -> Self {
Self {
input,
treat_string_literals_as_cstring_literals: true,
}
}

pub fn parse(&mut self) -> Result<AstFile, ParseError> {
Expand Down
9 changes: 8 additions & 1 deletion src/parser/parse_expr/primary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
modifier: StringModifier::Normal,
..
}) => Ok(Expr::new(
ExprKind::String(self.input.advance().kind.unwrap_string().value),
if self.treat_string_literals_as_cstring_literals {
ExprKind::NullTerminatedString(
CString::new(self.input.advance().kind.unwrap_string().value)
.expect("valid null-terminated string"),
)
} else {
ExprKind::String(self.input.advance().kind.unwrap_string().value)
},
source,
)),
TokenKind::OpenParen => {
Expand Down
2 changes: 1 addition & 1 deletion src/pragma_section/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl PragmaSection {

input.ignore_newlines();

let mut parser = parser::Parser::new(input);
let mut parser = parser::Parser::new_for_pragma(input);
let mut ast_file = AstFile::new();

if parser.input.eat(TokenKind::OpenCurly) {
Expand Down
14 changes: 7 additions & 7 deletions tests/raylib/_.adept
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

pragma => {
adept(c"3.0")
link(c"libraylib.a")
linkFramework(c"OpenGL")
linkFramework(c"Cocoa")
linkFramework(c"IOKit")
linkFramework(c"Foundation")
linkFramework(c"CoreFoundation")
adept("3.0")
link("libraylib.a")
linkFramework("OpenGL")
linkFramework("Cocoa")
linkFramework("IOKit")
linkFramework("Foundation")
linkFramework("CoreFoundation")
}

4 changes: 2 additions & 2 deletions tests/raylib/main.adept
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

func main {
screen_width int = 800
screen_height int = 450
screen_width := 800
screen_height := 450

InitWindow(screen_width, screen_height, c"Example Window!")

Expand Down

0 comments on commit f56eba3

Please sign in to comment.