Skip to content

Commit

Permalink
Adds C API no big deal.
Browse files Browse the repository at this point in the history
  • Loading branch information
lavignes committed Dec 4, 2012
1 parent 44c74c4 commit 01e8f85
Show file tree
Hide file tree
Showing 17 changed files with 498 additions and 51 deletions.
3 changes: 3 additions & 0 deletions configure.ac
@@ -1,5 +1,6 @@
AC_INIT([hkl], [0.0], [], [hkl], [http://github.com/hkl/hkl]) AC_INIT([hkl], [0.0], [], [hkl], [http://github.com/hkl/hkl])



AC_CONFIG_MACRO_DIR([m4]) AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_FILES([Makefile src/Makefile]) AC_CONFIG_FILES([Makefile src/Makefile])


Expand All @@ -9,5 +10,7 @@ AC_PROG_CC
AM_PROG_LEX AM_PROG_LEX
AC_PROG_YACC AC_PROG_YACC


AC_CHECK_LIB(dl, dlopen, LIBDL="-ldl")

AC_OUTPUT AC_OUTPUT


151 changes: 151 additions & 0 deletions examples/joust.hkl
@@ -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"))
15 changes: 2 additions & 13 deletions examples/math.hkl
@@ -1,14 +1,3 @@
Math = {} include "src/module/math.hkl"
Math.PI = 3.1415926
Math.TAU = Math.PI * 2


Math.abs = function(n) puts Math.pow(3.0, 2.0)

if n < 0
return 0-n
end

return n
end

puts Math.abs(Math.PI)
45 changes: 45 additions & 0 deletions examples/nprime.hkl
@@ -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)
17 changes: 17 additions & 0 deletions examples/tomato.hkl
@@ -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)
14 changes: 10 additions & 4 deletions src/Makefile.am
@@ -1,9 +1,10 @@
AM_CFLAGS = -Wall -Wextra -Werror -Wno-unused-parameter -Wno-unused-function -std=gnu99 AM_CFLAGS = -Wall -Wextra -Werror -Wno-unused-parameter -Wno-unused-function -std=gnu99
AM_LDFLAGS =
AM_YFLAGS = -d -v AM_YFLAGS = -d -v
AM_LFLAGS = 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_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 \ hkl_deque.c hklr.c hklr_object.c hklr_expression.c hklr_statement.c \
Expand Down Expand Up @@ -34,5 +35,10 @@ statement/hklr_statement_if.c \
statement/hklr_statement_while.c \ statement/hklr_statement_while.c \
linenoise/linenoise.c linenoise/linenoise.c


testbed_SOURCES = ${hkl_SOURCES} math_SOURCES = module/math.c
testbed_SOURCES += test/traversal.c test/hash.c math_SOURCES += hklr.c hkl_value.c hklr_expression.c
math_LDADD = -lm
math_CFLAGS = $(AM_CFLAGS)
math_CFLAGS += -shared -nostartfiles -fPIC


4 changes: 4 additions & 0 deletions src/expression/hklr_as_string.c
Expand Up @@ -75,6 +75,10 @@ HklValue* hklr_as_string(HklValue* value)
cast->as.string = hkl_string_new_from_utf8("func"); cast->as.string = hkl_string_new_from_utf8("func");
break; break;


case HKL_TYPE_CFUNC:
cast->as.string = hkl_string_new_from_utf8("cfunc");
break;

default: default:
assert(false); assert(false);
break; break;
Expand Down
5 changes: 5 additions & 0 deletions src/hkl.c
Expand Up @@ -22,6 +22,7 @@ HklList* var_stack;
HklList* closure_stack; HklList* closure_stack;
HklList* id_stack; HklList* id_stack;
HklList* pair_stack; HklList* pair_stack;
HklList* interface_stack;


typedef struct yy_buffer_state yy_buffer_state; typedef struct yy_buffer_state yy_buffer_state;
extern yy_buffer_state* yy_scan_string(const char*); extern yy_buffer_state* yy_scan_string(const char*);
Expand Down Expand Up @@ -129,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("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("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("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("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("true", NULL));
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("false", NULL)); hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("false", NULL));
Expand All @@ -140,6 +142,7 @@ int main(int argc, const char* argv[])
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("typeof", 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("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("return", NULL));
hkl_tree_move_pair(keywords_map, hkl_pair_new_from_utf8("interface", NULL));


// If there is a filename // If there is a filename
if (argv[1]) if (argv[1])
Expand All @@ -165,6 +168,7 @@ int main(int argc, const char* argv[])
closure_stack = hkl_list_new(); closure_stack = hkl_list_new();
id_stack = hkl_list_new(); id_stack = hkl_list_new();
pair_stack = hkl_list_new(); pair_stack = hkl_list_new();
interface_stack = hkl_list_new();


// Parse files normally // Parse files normally
if (interactive == false) if (interactive == false)
Expand Down Expand Up @@ -205,6 +209,7 @@ int main(int argc, const char* argv[])
hkl_list_free(closure_stack); hkl_list_free(closure_stack);
hkl_list_free(id_stack); hkl_list_free(id_stack);
hkl_list_free(pair_stack); hkl_list_free(pair_stack);
hkl_list_free(interface_stack);


hklr_shutdown(); hklr_shutdown();


Expand Down

0 comments on commit 01e8f85

Please sign in to comment.