Skip to content

Commit

Permalink
#157. Random functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
AZHenley committed Apr 28, 2019
1 parent f6dfcca commit e2853ba
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
14 changes: 13 additions & 1 deletion builtin/stl.knox
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 9 additions & 3 deletions emitter/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ func Generate(node *ast.Node) string {
func header() string {
code := ""
code += "#include <stdlib.h>\n#include <stdio.h>\n#include <string.h>\n#include <stdint.h>\n#include <stdbool.h>\n#include <stddef.h>\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()

Expand Down Expand Up @@ -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":
Expand All @@ -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]) + ")"
}

}
Expand Down
14 changes: 14 additions & 0 deletions emitter/knoxutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdint.h>
#include <stdbool.h>

// Concatensate two strings.
char* concat(const char *s1, const char *s2)
{
const size_t len1 = strlen(s1);
Expand All @@ -15,11 +16,24 @@ 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);
char *result = malloc(len + 1);
// 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);
}

0 comments on commit e2853ba

Please sign in to comment.