Skip to content

Commit

Permalink
fixed structs
Browse files Browse the repository at this point in the history
  • Loading branch information
CreggEgg committed Jul 25, 2024
1 parent dbfe90d commit bbb6d4a
Show file tree
Hide file tree
Showing 8 changed files with 650 additions and 5 deletions.
614 changes: 614 additions & 0 deletions ;

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion core.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>

extern int64_t printnum(int64_t *);
extern void printfloat(double);
extern void printint(int64_t);
extern void printstr(int64_t *);
extern int64_t getindex(int64_t *, int64_t);
Expand Down Expand Up @@ -38,6 +39,11 @@ int64_t copy(int64_t *a, int64_t *b, int64_t c) {
printf("size: %d\n", c);
return (int64_t)memcpy(a, b, c);
}
void printfloat(double x) {
printf("%x\n", x);
printf("%b\n", x);
printf("%lf\n", &x);
};
//
// printnum(pointer);
//
Expand Down
5 changes: 5 additions & 0 deletions floats.ct
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alias string = [int];
extern printfloat: fn(int): unit;
let main = fn() {
printfloat(5);
}
1 change: 1 addition & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub struct AnnotatedIdent {
#[derive(Debug, Clone)]
pub enum Literal {
Int(i32),
Float(f64),
String(String),
Struct(Vec<(String, UntypedExpr)>),
Function {
Expand Down
20 changes: 17 additions & 3 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,14 @@ fn compile_global(
r#type,
heap: false, // storage_location: StorageLocation::Heap,
}),
TypedLiteral::Float(x) => Ok(CitrusValue::Value {
cranelift_value: {
let floating = builder.ins().f64const(x);
builder.ins().bitcast(I64, MemFlags::new(), floating)
},
r#type,
heap: false, // storage_location: StorageLocation::Heap,
}),
},
TypedExpr::Ident(_, _) => todo!(),
TypedExpr::UnaryOp { .. } => todo!(),
Expand Down Expand Up @@ -797,8 +805,6 @@ fn compile_expr(
64 * (pairs.len() as u32 + 1),
));
let addr = builder.ins().stack_addr(I64, stack_slot, 0);
let len = builder.ins().iconst(I64, pairs.len() as i64);
builder.ins().store(MemFlags::new(), len, addr, 0);

for (i, (_, val)) in pairs.iter().enumerate() {
let val = compile_expr(
Expand All @@ -812,7 +818,7 @@ fn compile_expr(
.value(builder, obj_module)?;
builder
.ins()
.store(MemFlags::new(), val, addr, (i + 1) as i32 * 64);
.store(MemFlags::new(), val, addr, (i) as i32 * 64);
}
CitrusValue::Value {
cranelift_value: addr,
Expand All @@ -825,6 +831,14 @@ fn compile_expr(
r#type,
heap: false, // storage_location: StorageLocation::Stack,
},
TypedLiteral::Float(x) => CitrusValue::Value {
cranelift_value: {
let floating = builder.ins().f64const(x);
builder.ins().bitcast(I64, MemFlags::new(), floating)
},
r#type,
heap: false,
},
},
scope,
)),
Expand Down
1 change: 1 addition & 0 deletions src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ FunctionArgs: Vec<UntypedExpr> = "(" <a: Comma<Expr>> ")" => a;
Ident: String = r"[a-zA-Z_]+" => <>.to_owned();
Literal: Literal = {
r"[0-9]+" => Literal::Int(<>.parse::<i32>().unwrap()),
r"[0-9]+\.[0-9]+" => Literal::Float(<>.parse::<f64>().unwrap()),
"false" => Literal::Bool(false),
"true" => Literal::Bool(true),
<s: r#"".+""#> => Literal::String(s[1..(s.len() - 1)].to_owned()),
Expand Down
5 changes: 4 additions & 1 deletion src/types/inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ pub enum TypeError {
pub fn type_file(ast: File) -> Result<TypedFile, TypeError> {
let mut types: HashMap<String, Type> = HashMap::new();
types.insert("int".into(), Type::Int);
types.insert("float".into(), Type::Float);
types.insert("bool".into(), Type::Bool);
types.insert("unit".into(), Type::Float);
types.insert("unit".into(), Type::Unit);
let mut scope: HashMap<String, Type> = HashMap::new();
let mut declarations = Vec::new();
for declaration in ast.declarations {
Expand Down Expand Up @@ -409,6 +410,7 @@ fn type_literal(
) -> Result<TypedLiteral, TypeError> {
Ok(match literal {
crate::ast::Literal::Int(x) => TypedLiteral::Int(*x),
crate::ast::Literal::Float(x) => TypedLiteral::Float(*x),
crate::ast::Literal::Bool(x) => TypedLiteral::Bool(*x),
crate::ast::Literal::Array(x) => {
let typed = x
Expand Down Expand Up @@ -493,6 +495,7 @@ fn get_literal_type(
) -> Result<Type, TypeError> {
match literal {
crate::ast::Literal::Int(_) => Ok(Type::Int),
crate::ast::Literal::Float(_) => Ok(Type::Float),
crate::ast::Literal::Bool(_) => Ok(Type::Bool),
crate::ast::Literal::Struct(pairs) => Ok(Type::Struct(
pairs
Expand Down
1 change: 1 addition & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub enum TypedLiteral {
Array(Vec<TypedExpr>),
Struct(Type, Vec<(String, TypedExpr)>),
Unit,
Float(f64),
}

#[derive(Debug, PartialEq, Clone)]
Expand Down

0 comments on commit bbb6d4a

Please sign in to comment.