Skip to content

Commit

Permalink
Strings and so much more
Browse files Browse the repository at this point in the history
  • Loading branch information
cody-quinn committed Jun 27, 2023
1 parent 8c9bd55 commit a3c5134
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 132 deletions.
6 changes: 3 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Build Sloth
cargo build --features=llvm-sys/prefer-dynamic
cargo build

# Compile standard library
./target/debug/sloth std/stdio.sloth
mv output.o stdio.o
./target/debug/sloth std/stdlib.sloth
mv output.o stdlib.io
mv output.o stdlib.o
./target/debug/sloth std/stdmath.sloth
mv output.o stdmath.o

Expand All @@ -14,4 +14,4 @@ mv output.o stdmath.o
mv output.o main.o

# Generate binary
gcc stdio.o std/stdio.c stdlib.o std/stdlib.c stdmath.o std/stdmath.c main.o -o program
clang stdio.o std/stdio.c stdlib.o std/stdlib.c stdmath.o std/stdmath.c main.o -o program
40 changes: 3 additions & 37 deletions examples/hello.sloth
Original file line number Diff line number Diff line change
@@ -1,40 +1,6 @@
fn test() [Int] {
var list: [Int] = [500, 5, 7];
foreign fn print(x: String) Void;

vpushi(list, 3);
vpushi(list, 3);
vpushi(list, 3);
vpushi(list, 5);

var x: Int = vpopi(list);
vpushi(list, x);
vpushi(list, x * 2);
vpushi(list, x * 3);

return list;
}

fn testtwo(list: [Int]) Int {
#vpopi(list);
var x: Int = vpopi(list);
return x;
}

fn testthree(list: [Int]) Int {
var x: Int = vlen(list);
return x;
}

foreign fn testback(x: Int) Void;

fn testfour(list: [Int]) Int {
vseti(list, 0, 888);
var i: Int = 0;
while i < vlen(list) {
var value: Int = vgeti(list, i);
testback(value);
i = i + 1;
}
fn main() Int {
print("gaming\n");
return 0;
}

25 changes: 14 additions & 11 deletions examples/mandelbrot.sloth
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
foreign fn termpos(x: Int, y: Int);
foreign fn print(str: String);
foreign fn printint(i: Int);
foreign fn as_int(x: Float) Int;

foreign fn termpos(x: Int, y: Int) Void;

fn main() Int{
var size: Float = 200.0;
var maxVal: Float = 4.0;
var maxIter: Int = 50;
var maxIter: Float = 50.0;
var plane: Float = 4.0;
# lmao
var x: Int = 0;
var x: Float = 0.0;
while x < size {
var y: Int = 0;
var y: Float = 0.0;
while y < size {
var cReal: Float = (x * plane / size) - 2.0;
var cImg: Float = (y * plane / size) - 2.0;
var zReal: Float = 0.0;
var zImg: Float = 0.0;
var count: Int = 0;
while (zReal * zReal + zImg * zImg) <= maxVal && count < 4{
var count: Float = 0.0;
while (zReal * zReal + zImg * zImg) <= maxVal && count < maxIter {
var temp: Float = (zReal * zReal) - (zImg * zImg) + cReal;
zImg = 2.0 * zReal * zImg + cImg;
zReal = temp;
count = count + 1;
count = count + 1.0;
}
if count == maxIter {
termpos(x, y);
if as_int(count) == as_int(maxIter) {
termpos(as_int(x), as_int(y));
print("*");
}
y = y + 1;
y = y + 1.0;
}
x = x + 1;
x = x + 1.0;
}
return 0;
}
66 changes: 51 additions & 15 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
};

rustStable = pkgs.rust-bin.stable.latest.default;
rustNightly = pkgs.rust-bin.nightly."2023-02-10".default;
rustNightly = pkgs.rust-bin.nightly."2023-06-19".default;

rustPlatform = pkgs.makeRustPlatform {
cargo = rustStable;
Expand Down
31 changes: 25 additions & 6 deletions sloth/src/analysis/setup.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::AnalysisError;
use crate::parser::ast::{
AstNode, Expr, ExprKind, Function, FunctionInput, FunctionKind, Literal, Stmt, StmtKind,
TypeIdentifier,
AstNode, BinaryOp, Expr, ExprKind, Function, FunctionInput, FunctionKind, Literal, Stmt,
StmtKind, TypeIdentifier,
};
use crate::symtable::{Symbol, SymbolTable, Type, ValueSymbol};

Expand Down Expand Up @@ -181,6 +181,7 @@ pub(super) fn propagate_types(node: &mut Expr) -> Result<(), AnalysisError> {

last.expect("need 1 element in literal im sozzy")
}
Literal::String(_) => Type::String,
_ => todo!(),
},
ExprKind::Identifier(identifier) => {
Expand All @@ -189,7 +190,7 @@ pub(super) fn propagate_types(node: &mut Expr) -> Result<(), AnalysisError> {
AnalysisError::UnknownIdentifier(node.line, identifier.to_owned()),
)?
}
ExprKind::BinaryOp { lhs, rhs, .. } => {
ExprKind::BinaryOp { lhs, rhs, op } => {
// Propagating the types to the children
propagate_types(lhs)?;
propagate_types(rhs)?;
Expand All @@ -198,9 +199,27 @@ pub(super) fn propagate_types(node: &mut Expr) -> Result<(), AnalysisError> {
return Err(AnalysisError::TypeMismatch(node.line));
}

lhs.typ
.clone()
.ok_or(AnalysisError::Unknown(node.line, "owo?? choco???"))?
match op {
BinaryOp::Add
| BinaryOp::Con
| BinaryOp::Sub
| BinaryOp::Mul
| BinaryOp::Div
| BinaryOp::Mod => lhs
.typ
.clone()
.ok_or(AnalysisError::Unknown(node.line, "owo?? choco???"))?,
BinaryOp::Lt
| BinaryOp::Gt
| BinaryOp::LtEq
| BinaryOp::GtEq
| BinaryOp::EqEq
| BinaryOp::NotEq => Type::Boolean,
BinaryOp::LogicalAnd | BinaryOp::LogicalOr | BinaryOp::Range => lhs
.typ
.clone()
.ok_or(AnalysisError::Unknown(node.line, "owo?? choco???"))?,
}
}
ExprKind::UnaryOp { value, .. } => {
propagate_types(value)?;
Expand Down
Loading

0 comments on commit a3c5134

Please sign in to comment.