Permalink
Browse files

Merge pull request #115 from pyrated/master

Adds C API no big deal
  • Loading branch information...
2 parents a31e842 + 01e8f85 commit 260aa1cdfa83cb2163d37580a4385f70b017894c @bahostetterlewis bahostetterlewis committed Dec 4, 2012
View
@@ -1,5 +1,6 @@
AC_INIT([hkl], [0.0], [], [hkl], [http://github.com/hkl/hkl])
+
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_FILES([Makefile src/Makefile])
@@ -9,5 +10,7 @@ AC_PROG_CC
AM_PROG_LEX
AC_PROG_YACC
+AC_CHECK_LIB(dl, dlopen, LIBDL="-ldl")
+
AC_OUTPUT
View
@@ -0,0 +1,151 @@
+include "src/module/math.hkl"
+
+Knight = {
+
+ new = function(name, stamina, weapon, armor, shield)
+
+ return {
+ name: name? "Starry Knight",
+ stamina: stamina? 0,
+ weapon: weapon,
+ armor: armor,
+ shield: shield
+ }
+ end
+}
+
+Weapon = {
+ new = function(name, stamina, hit_chance)
+ return {name: name, stamina: stamina, hit_chance: hit_chance}
+ end
+}
+
+//-- Borrow new function from Weapon
+Armor = { new = Weapon.new }
+Shield = { new = Armor.new }
+
+puts "Joust Tester"
+
+init = function(first_or_second)
+
+ puts "What is the name of the " + first_or_second + " knight?"
+ kname = gets
+ puts "How many stamina points does he have?"
+ kstamina = gets as real
+
+ puts "What is the name of his weapon?"
+ name = gets
+ puts "What is the hit ratio (0 - 100)% of the weapon?"
+ hit_chance = gets as real
+ puts "How many stamina points does it take to wield such a weapon?"
+ weapon1 = Weapon.new(name, gets as real, hit_chance)
+
+ puts "What is the name of his armor?"
+ name = gets
+ puts "What is the hit reduction ratio (0 - 100)% of the armor?"
+ hit_chance = gets as real
+ puts "How many stamina points does it take to wear?"
+ armor1 = Armor.new(name, gets as real, hit_chance)
+
+ puts "What is the name of his shield?"
+ name = gets
+ puts "What is the hit reduction ratio (0 - 100)% of the shield?"
+ hit_chance = gets as real
+ puts "How many stamina points does it take to carry?"
+ shield1 = Armor.new(name, gets as real, hit_chance)
+
+ return Knight.new(kname, kstamina, weapon1, armor1, shield1)
+end
+
+Joust = {}
+Joust.match = function(knight1, knight2)
+
+ Joust.knight1 = knight1
+ Joust.knight2 = knight2
+ Joust.num_rounds = 0
+
+ while Joust.round() end
+end
+
+Joust.round = function()
+
+ k1falls = false
+ k2falls = false
+
+ puts "Round " + Joust.num_rounds
+
+ newstam = knight1.stamina
+ - knight1.weapon.stamina
+ - knight1.armor.stamina
+ - knight1.shield.stamina
+
+ if newstam < 0 newstam = 0 end
+
+ knight1.stamina = newstam
+
+ newstam = knight2.stamina
+ - knight2.weapon.stamina
+ - knight2.armor.stamina
+ - knight2.shield.stamina
+
+ if newstam < 0 newstam = 0 end
+
+ knight2.stamina = newstam
+
+ chance = 0 - knight1.armor.hit_chance
+ - knight1.shield.hit_chance
+ + knight2.weapon.hit_chance
+
+ if chance < 0 chance = 0 end
+
+ puts "\t" + knight1.name + " has a " + chance + "% chance to fall off."
+
+ if Math.random()%100 <= chance
+ k1falls = true
+ end
+
+ chance = 0 - knight2.armor.hit_chance
+ - knight2.shield.hit_chance
+ + knight1.weapon.hit_chance
+
+ if chance < 0 chance = 0 end
+
+ puts "\t" + knight2.name + " has a " + chance + "% chance to fall off."
+
+ if Math.random()%100 <= chance
+ k2falls = true
+ end
+
+ if k1falls
+ puts "\t" + knight2.name + "'s " + knight2.weapon.name + " threw " + knight1.name + "off his horse."
+ end
+
+ if k2falls
+ puts "\t" + knight1.name + "'s " + knight1.weapon.name + " threw " + knight2.name + "off his horse."
+ end
+
+ if k1falls == knight1.stamina
+ puts "\t" + knight1.name + " collapsed from exhaustion!"
+ k1falls = true
+ end
+
+ if k2falls == knight2.stamina
+ puts "\t" + knight2.name + " collapsed from exhaustion!"
+ k2falls = true
+ end
+
+ if k1falls
+ return false
+ end
+
+ if k2falls
+ return false
+ end
+
+ Joust.num_rounds = Joust.num_rounds + 1
+
+ return true
+
+end
+
+Joust.match(init("first"), init("second"))
View
@@ -1,14 +1,3 @@
-Math = {}
-Math.PI = 3.1415926
-Math.TAU = Math.PI * 2
+include "src/module/math.hkl"
-Math.abs = function(n)
-
- if n < 0
- return 0-n
- end
-
- return n
-end
-
-puts Math.abs(Math.PI)
+puts Math.pow(3.0, 2.0)
View
@@ -0,0 +1,45 @@
+sqrt = function(n)
+
+ i = (n / 2) + 1
+ j = (i + (n / i)) / 2
+
+ while j < i
+ i = j
+ j = (i + (n / i)) / 2
+ end
+
+ return i
+end
+
+isprime = function(n)
+
+ sqn = sqrt(n)
+
+ ok = true
+
+ m = 2 while m <= sqn
+
+ if n % m == 0 ok = false break end
+ m = m + 1
+
+ end
+
+ return ok
+
+end
+
+puts "What prime?"
+n = gets as int
+
+j = 2
+prime = 0 while prime < n
+
+ if isprime(j)
+ prime = prime + 1
+ end
+
+ j = j + 1
+
+end
+
+puts "The " + n +"th prime is " + (j - 1)
View
@@ -0,0 +1,17 @@
+Tomato = {}
+
+Tomato.Color = {red, green, blue, yellow}
+
+Tomato.new = function(color)
+ return {color: color? Tomato.Color.red}
+end
+
+Tomato.throw = function(tmto)
+ puts "Threw a " + tmto.color + " tomato"
+end
+
+tmto = Tomato.new()
+tmto2 = Tomato.new(Tomato.Color.blue)
+
+Tomato.throw(tmto)
+Tomato.throw(tmto2)
View
@@ -1,9 +1,10 @@
AM_CFLAGS = -Wall -Wextra -Werror -Wno-unused-parameter -Wno-unused-function -std=gnu99
-AM_LDFLAGS =
AM_YFLAGS = -d -v
AM_LFLAGS =
-bin_PROGRAMS = hkl testbed
+bin_PROGRAMS = hkl math
+
+hkl_LDADD = -ldl
hkl_SOURCES = y.tab.y lex.yy.l hkl_string.c hkl_hash.c hkl_tree.c hkl.c \
hkl_deque.c hklr.c hklr_object.c hklr_expression.c hklr_statement.c \
@@ -34,5 +35,10 @@ statement/hklr_statement_if.c \
statement/hklr_statement_while.c \
linenoise/linenoise.c
-testbed_SOURCES = ${hkl_SOURCES}
-testbed_SOURCES += test/traversal.c test/hash.c
+math_SOURCES = module/math.c
+math_SOURCES += hklr.c hkl_value.c hklr_expression.c
+math_LDADD = -lm
+math_CFLAGS = $(AM_CFLAGS)
+math_CFLAGS += -shared -nostartfiles -fPIC
+
+
@@ -75,6 +75,10 @@ HklValue* hklr_as_string(HklValue* value)
cast->as.string = hkl_string_new_from_utf8("func");
break;
+ case HKL_TYPE_CFUNC:
+ cast->as.string = hkl_string_new_from_utf8("cfunc");
+ break;
+
default:
assert(false);
break;
@@ -4,8 +4,7 @@
HklValue* hklr_op_typeof(HklValue* value)
{
-
- //bool temporary = true;
+ bool temporary = true;
// Dereference
if (value->type == HKL_TYPE_REF)
@@ -15,9 +14,9 @@ HklValue* hklr_op_typeof(HklValue* value)
value = hklr_object_dereference(value->as.object);
// Don't free the deque or hash since it can't be a temporary
- if (value->type == HKL_TYPE_ARRAY)
+ if (value->type == HKL_TYPE_ARRAY || value->type == HKL_TYPE_HASH || value->type == HKL_TYPE_FUNCTION)
{
- //temporary = false;
+ temporary = false;
// simply spoof the value
temp->type = HKL_TYPE_NIL;
}
@@ -26,6 +25,11 @@ HklValue* hklr_op_typeof(HklValue* value)
}
HklType type = value->type;
+
+ // Spoof the value if it not temporary
+ if (temporary == false)
+ value->type = HKL_TYPE_NIL;
+
hkl_value_free(value);
value = hkl_value_new(HKL_TYPE_TYPE, type);
View
@@ -22,6 +22,7 @@ HklList* var_stack;
HklList* closure_stack;
HklList* id_stack;
HklList* pair_stack;
+HklList* interface_stack;
typedef struct yy_buffer_state yy_buffer_state;
extern yy_buffer_state* yy_scan_string(const char*);
@@ -119,6 +120,7 @@ int main(int argc, const char* argv[])
keywords_map = hkl_tree_new();
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("if", NULL));
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("while", NULL));
+ hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("break", NULL));
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("end", NULL));
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("include", NULL));
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("nil", NULL));
@@ -128,6 +130,7 @@ int main(int argc, const char* argv[])
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("array", NULL));
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("hash", NULL));
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("func", NULL));
+ hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("cfunc", NULL));
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("type", NULL));
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("true", NULL));
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("false", NULL));
@@ -138,6 +141,8 @@ int main(int argc, const char* argv[])
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("collect", NULL));
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("typeof", NULL));
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("function", NULL));
+ hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("return", NULL));
+ hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("interface", NULL));
// If there is a filename
if (argv[1])
@@ -163,6 +168,7 @@ int main(int argc, const char* argv[])
closure_stack = hkl_list_new();
id_stack = hkl_list_new();
pair_stack = hkl_list_new();
+ interface_stack = hkl_list_new();
// Parse files normally
if (interactive == false)
@@ -203,6 +209,7 @@ int main(int argc, const char* argv[])
hkl_list_free(closure_stack);
hkl_list_free(id_stack);
hkl_list_free(pair_stack);
+ hkl_list_free(interface_stack);
hklr_shutdown();
View
@@ -86,6 +86,10 @@ void hkl_value_free(HklValue* value)
hkl_hash_free(value->as.hash);
break;
+ case HKL_TYPE_FUNCTION:
+ hklr_function_free(value->as.function);
+ break;
+
default:
break;
}
Oops, something went wrong.

0 comments on commit 260aa1c

Please sign in to comment.