Skip to content

Commit

Permalink
Merge branch 'astaugaard-main'
Browse files Browse the repository at this point in the history
  • Loading branch information
CreggEgg committed Jul 23, 2024
2 parents 39a4a39 + 5aa0d90 commit 71b8068
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/out
79 changes: 68 additions & 11 deletions new.ct
Original file line number Diff line number Diff line change
@@ -1,18 +1,75 @@
alias string = [int];
extern printstr: fn(string): unit;
extern printint: fn(int): unit;
extern index: fn([int], int): int;
extern setindex: fn([int], int, int): unit;
extern setindex: fn([int],int,int): unit;

let greeting = fn(): string {
let str = "hello world";
printstr(str);
str
let isqrt_go = fn(a: int, s: int): int {
let a_ = (a + s / a) / 2;

if a_ >= a {
a
} else {
isqrt_go(a_,s)
}
};

let isqrt = fn(a: int): int {
isqrt_go(a,a)
};


let gcd = fn(a: int,b:int): int {
if b > a {
gcd(b,a)
} else {
if b == 0 {
a
} else {
gcd(a, b % a)
}
}
};

let fraction_root_go = fn(cur_ind: int, list: [int], root_value: int,
numerator_plus: int, divisor: int, stop_at: int,
len: int): int {
let an = (isqrt(root_value) + numerator_plus) / 2;

if an == stop_at {
cur_ind - 1
} else {
if cur_ind >= len {
printstr("value won't be correct becasue not enough array space was allocated");
-1
} else {
setindex(list,cur_ind,an);

let denom = (root_value -
numerator_plus * numerator_plus -
an * an * divisor * divisor +
2 * an * divisor * numerator_plus);

let gcd = gcd(divisor,denom);
}
}
};


let fraction_root = fn(a: int, list: [int], len: int): int {
let a_zero = isqrt(a);

setindex(a,0,a_zero);

if (a_zero * a_zero == a) {
0
} else {
fraction_root_go(1,list,a,a_zero,(a - a_zero * a_zero), a_zero * 2, len)
}
};

let main = fn() {
let x = [64, 65, 64];
printint(index(x, 1));
setindex(x, 1, 64);
printint(index(x, 1));
printstr(greeting())
printint(isqrt(10));
printint(isqrt(100));
printint(isqrt(4));
printint(isqrt(201));
};
Binary file removed out/main
Binary file not shown.
20 changes: 20 additions & 0 deletions project_euler1.ct
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
alias string = [int];
extern printstr: fn(string): unit;
extern printint: fn(int): unit;

let sum_to = fn(a: int): int {
let ugly_hack = a + 1;
let num = a * ugly_hack;
num/2
};

let three_five_sum = fn(a: int): int {
let a = a - 1;

sum_to(a/3) * 3 + sum_to(a/5) * 5 - sum_to(a/15) * 15
};

let main = fn() {
printstr("result: ");
printint(three_five_sum(1000))
};
62 changes: 42 additions & 20 deletions src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,60 @@ StructExprPair: (String, UntypedExpr) = <i: Ident> ":" <e: Expr> => (i, e);

Block: Vec<UntypedExpr> = Semicolon<Expr> => <>;
Expr: UntypedExpr = {
#[precedence(level="11")]
<l: Expr> "::" <i: Ident> => UntypedExpr::Access(Box::new(l), i),
#[precedence(level="10")]
"let" <l: "local"?> <i: Ident> "=" <e: Expr> => UntypedExpr::Binding {lhs: i, local: l.is_some(), rhs: Box::new(e)},
#[precedence(level="9")]
<i: Ident> "=" <e: Expr> => UntypedExpr::Mutate {lhs: i, rhs: Box::new(e)},
#[precedence(level="0")]
Literal => UntypedExpr::Literal(<>),
#[precedence(level="2")] #[assoc(side="left")]
<a: Atom> => a,

#[precedence(level="1")] #[assoc(side="left")]
<l: Expr> "::" <i: Ident> => UntypedExpr::Access(Box::new(l), i),
<i: Ident> <a: FunctionArgs> => UntypedExpr::FunctionCall(i, a),

#[precedence(level="2")] #[assoc(side="right")]
"!" <e: Expr> => UntypedExpr::UnaryOp {op: UnaryOperator::Not, target: Box::new(e)},
"-" <e: Expr> => UntypedExpr::UnaryOp {op: UnaryOperator::Negative, target: Box::new(e)},

#[precedence(level="3")] #[assoc(side="left")]
<l: Expr> "*" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Multiply, rhs: Box::new(r)},
<l: Expr> "/" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Divide, rhs: Box::new(r)},
<l: Expr> "%" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Modulo, rhs: Box::new(r)},

#[precedence(level="4")] #[assoc(side="left")]
<l: Expr> "+" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Add, rhs: Box::new(r)},
<l: Expr> "-" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Subtract, rhs: Box::new(r)},

#[precedence(level="5")] #[assoc(side="left")]
<l: Expr> ">" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Gt, rhs: Box::new(r)},
<l: Expr> "<" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Lt, rhs: Box::new(r)},
<l: Expr> ">=" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Gte, rhs: Box::new(r)},
<l: Expr> "<=" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Lte, rhs: Box::new(r)},
<l: Expr> "%" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Modulo, rhs: Box::new(r)},
#[precedence(level="3")] #[assoc(side="left")]
<l: Expr> "*" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Multiply, rhs: Box::new(r)},
<l: Expr> "/" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Divide, rhs: Box::new(r)},

#[precedence(level="6")] #[assoc(side="left")]
<l: Expr> "==" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Eq, rhs: Box::new(r)},
<l: Expr> "!=" <r: Expr> => UntypedExpr::UnaryOp
{op: UnaryOperator::Negative,
target: Box::new(UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Eq, rhs: Box::new(r)})
},

#[precedence(level="7")] #[assoc(side="left")]
<l: Expr> "&&" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::And, rhs: Box::new(r)},

#[precedence(level="8")] #[assoc(side="left")]
<l: Expr> "||" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Or, rhs: Box::new(r)},
#[precedence(level="5")]
<i: Ident> <a: FunctionArgs> => UntypedExpr::FunctionCall(i, a),
#[precedence(level="1")] #[assoc(side="left")]
<i: Ident> => UntypedExpr::Ident(i),
"if" <c: Expr> "{" <t: Block> "}" "else" "{" <e: Block> "}" => UntypedExpr::IfElse { condition: Box::new(c), then: t, r#else: e},
#[precedence(level="4")]

#[precedence(level="9")] #[assoc(side="right")]
"@" <e: Expr> => UntypedExpr::UnaryOp {op: UnaryOperator::Exclave, target: Box::new(e)},
"!" <e: Expr> => UntypedExpr::UnaryOp {op: UnaryOperator::Not, target: Box::new(e)},
"-" <e: Expr> => UntypedExpr::UnaryOp {op: UnaryOperator::Negative, target: Box::new(e)},

#[precedence(level="10")] #[assoc(side="right")]
"if" <c: Expr> "{" <t: Block> "}" "else" "{" <e: Block> "}" => UntypedExpr::IfElse { condition: Box::new(c), then: t, r#else: e},
"let" <l: "local"?> <i: Ident> "=" <e: Expr> => UntypedExpr::Binding {lhs: i, local: l.is_some(), rhs: Box::new(e)},
<i: Ident> "=" <e: Expr> => UntypedExpr::Mutate {lhs: i, rhs: Box::new(e)},
}

Atom: UntypedExpr = {
<i: Ident> => UntypedExpr::Ident(i),
Literal => UntypedExpr::Literal(<>),
"(" <i: Expr> ")" => i,
}

Annotation: TypeName = ":" <i: Type> => i;
FunctionArgs: Vec<UntypedExpr> = "(" <a: Comma<Expr>> ")" => a;
Ident: String = r"[a-zA-Z_]+" => <>.to_owned();
Expand Down

0 comments on commit 71b8068

Please sign in to comment.