diff --git a/builtin/stl.knox b/builtin/stl.knox index e93992d..69a692f 100644 --- a/builtin/stl.knox +++ b/builtin/stl.knox @@ -1,12 +1,24 @@ -// TODO: stl should be a module, not a class. +// TODO: stl should be a module, not a class? class stl { + // Standard input and output. func print(x : string) void {} + + // File input and output. + + // List operations. func range(start : int, end : int, step : int) [int] { return [0]; } + // Bitwise. func and(x : int, y : int) int { return 0; } func or(x : int, y : int) int { return 0; } func not(x : int) int { return 0; } func xor(x : int, y : int) int { return 0; } func left(x : int, y : int) int { return 0; } func right(x : int, y : int) int { return 0; } + + // Math. + func random(min : int, max : int) int { return 0; } + func randomf(min : float, max : float) float { return 0; } + // func randomd(min : double, max : double) double { return 0; } + // func seed } \ No newline at end of file diff --git a/emitter/emitter.go b/emitter/emitter.go index 7424012..5ea3a5d 100644 --- a/emitter/emitter.go +++ b/emitter/emitter.go @@ -26,12 +26,10 @@ func Generate(node *ast.Node) string { func header() string { code := "" code += "#include \n#include \n#include \n#include \n#include \n#include \n#include \"knoxutil.h\"\n\n" // TODO: #130 Only include what is needed. + // TODO: Main should set seed. return code } -// TODO: Declare all functions first, before definitions, to avoid ordering issues. -// TODO: Main needs to return int. - func program(node *ast.Node) string { head := header() @@ -234,6 +232,8 @@ func funcCall(node *ast.Node) string { funcName = "printf" argList = expr(&node.Children[1]) return funcName + "(\"%s\", " + argList + ");" + + // Bitwise. case "and": return "(" + expr(&node.Children[1]) + " & " + expr(&node.Children[2]) + ")" case "or": @@ -246,6 +246,12 @@ func funcCall(node *ast.Node) string { return "(" + expr(&node.Children[1]) + " >> " + expr(&node.Children[2]) + ")" case "xor": return "(" + expr(&node.Children[1]) + " ^ " + expr(&node.Children[2]) + ")" + + // Math. + case "random": + return "random(" + expr(&node.Children[1]) + ", " + expr(&node.Children[2]) + ")" + case "randomf": + return "randomf(" + expr(&node.Children[1]) + ", " + expr(&node.Children[2]) + ")" } } diff --git a/emitter/knoxutil.h b/emitter/knoxutil.h index 6080194..88ed4a8 100644 --- a/emitter/knoxutil.h +++ b/emitter/knoxutil.h @@ -4,6 +4,7 @@ #include #include +// Concatensate two strings. char* concat(const char *s1, const char *s2) { const size_t len1 = strlen(s1); @@ -15,6 +16,7 @@ char* concat(const char *s1, const char *s2) return result; } +// Copy the contents of one string to a new string. char* copy(const char *s) { const size_t len = strlen(s); @@ -22,4 +24,16 @@ char* copy(const char *s) // TODO: Check for malloc errors. memcpy(result, s, len + 1); return result; +} + +int random(int min, int max) { + return (rand() % (max - min + 1)) + min; +} + +float randomf(float min, float max) { + return min + ((float)rand() / RAND_MAX) * (max - min); +} + +double randomd(double min, double max) { + return min + ((double)rand() / RAND_MAX) * (max - min); } \ No newline at end of file