Skip to content

Commit

Permalink
Merge branch 'master' of github.com:slothlang/sloth
Browse files Browse the repository at this point in the history
  • Loading branch information
cody-quinn committed Jun 26, 2023
2 parents ae4c1af + c3bb175 commit 91ac969
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 39 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cargo build --features=llvm-sys/prefer-dynamic
22 changes: 11 additions & 11 deletions examples/mandelbrot.sloth
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
val size: int = 200;
val maxVal: float = 4.0;
val maxIter: int = 50;
val plane: float = 4.0;
val size: Int = 200;
val maxVal: Float = 4.0;
val maxIter: Int = 50;
val plane: Float = 4.0;

for x in 0 .. size {
for y in 0 .. size {
var cReal: float = (x * plane / size) - 2;
var cImg: float = (y * plane / size) - 2;
var zReal: float = 0;
var zImg: float = 0;
var count: float = 0;
var cReal: Float = (x * plane / size) - 2;
var cImg: Float = (y * plane / size) - 2;
var zReal: Float = 0;
var zImg: Float = 0;
var count: Float = 0;
while (zReal * zReal + zImg * zImg) <= maxVal && count < 4{
var temp: float = (zReal * zReal) - (zImg * zImg) + cReal;
var temp: Float = (zReal * zReal) - (zImg * zImg) + cReal;
zImg = 2 * zReal * zImg + cImg;
zReal = temp;
count += 1;
}
if count == maxIter {
term_setpos(x, y);
termpos(x, y);
print("*");
}
}
Expand Down
4 changes: 4 additions & 0 deletions examples/mergesort.sloth
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn merge_sort(list: List<Int>) {
print(list);

}
1 change: 1 addition & 0 deletions sloth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version.workspace = true
edition.workspace = true

[dependencies]
llvm-sys = "150"
inkwell = { version = "0.2.0", features = ["llvm15-0"] }
itertools = "0.10.5"
rand = "0.8.5"
Expand Down
3 changes: 1 addition & 2 deletions sloth/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub mod analysis;
pub mod codegen;
pub mod lexer;
pub mod parser;
pub mod sloth_std;
pub mod symtable;

use std::fs::File;
Expand All @@ -26,7 +25,7 @@ use parser::AstParser;
use symtable::{Symbol, SymbolTable};

use crate::analysis::analyze;
use crate::parser::graph::GraphBuilder;

use crate::symtable::Type;

fn main() {
Expand Down
26 changes: 0 additions & 26 deletions sloth/src/sloth_std.rs

This file was deleted.

12 changes: 12 additions & 0 deletions std/stdio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>
#include <stdlib.h>

char* readln() {
char* str = malloc(128);
scanf("%127s", str);
return str;
}

void print(char *str) {
fputs(str, stdout);
}
9 changes: 9 additions & 0 deletions std/stdio.sloth
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
foreign fn print(str: String) Void;
foreign fn readln() String;

fn println(str: String) Void {
print(str);
print("\n");
}


19 changes: 19 additions & 0 deletions std/stdlib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

void wait(long long x) {
sleep(x);
}

long long slen(char *str) {
return (long long) strlen(str);
}

char charAt(char *str, long long x) {
return str[x];
}

long long parse_int(char *str) {
return (long long) atoi(str);
}
17 changes: 17 additions & 0 deletions std/stdlib.sloth
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
foreign fn wait(x: Int) Void;
foreign fn print(str: String) Void;
foreign fn slen(str: String) Int;
foreign fn charAt(str: String) Char;
foreign fn parse_int(str: String) Int;

fn termpos(x: int, y: int) Void {
print("\x1b[");
print(x);
print(";");
print(y);
print("H");
}

fn termclear() Void {
print("\x1b[2J\x1b[H");
}
9 changes: 9 additions & 0 deletions std/stdmath.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


long long randGen(long long min, long long max) {
srandom((unsigned) time(NULL));
return random() % (max - min + 1) + min;
}
69 changes: 69 additions & 0 deletions std/stdmath.sloth
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
foreign fn randGen(min: Int, max: Int) Int;

fn abs(x: Int) Int {
if x < 0 {
return -x;
}
return x;
}

fn fabs(x: Float) Float {
if x < 0.0 {
return -x;
}
return x;
}

fn max(x: Int, y: Int) Int {
if x > y {
return x;
}
return y;
}

fn min(x: Int, y: Int) Int {
if x < y {
return x;
}
return y;
}

fn fmax(x: Float, y: Float) Float {
if x > y {
return x;
}
return y;
}

fn fmin(x: Float, y: Float) Float {
if x < y {
return x;
}
return y;
}

fn pow(x: Int, y: Int) Int {
while y > 1 {
x = x*x;
y = y-1;
}
return x;
}

fn floor(x: Float) Float {
return x - fabs(x % 1.0);
}

fn ceil(x: Float) Float {
if x < 0.0 {
return floor(x) - 1.0;
}
return floor(x) + 1.0;
}

fn round(x: Float) Float {
if fabs(x % 1.0) >= 0.5 {
return ceil(x);
}
return floor(x);
}

0 comments on commit 91ac969

Please sign in to comment.