Permalink
Browse files

Adds broken array expression and 99bottles example

  • Loading branch information...
1 parent e1d58d3 commit 53c6bb7b2eec107c9d47234e5ddf8bd614805acb @pyrated pyrated committed Nov 9, 2012
Showing with 110 additions and 10 deletions.
  1. +37 −0 examples/99bottles.hkl
  2. +2 −0 src/hkl.c
  3. +4 −0 src/hkl_value.c
  4. +3 −1 src/hkl_value.h
  5. +29 −4 src/hklr_expression.c
  6. +4 −1 src/hklr_expression.h
  7. +17 −0 src/statement/hklr_statement_puts.c
  8. +14 −4 src/y.tab.y
View
@@ -0,0 +1,37 @@
+bottles = 99
+
+while (bottles)
+
+ if (bottles > 1)
+ puts bottles + " bottles of beer on the wall, "
+ puts bottles + " bottles of beer.\n"
+ end
+
+ if (bottles == 1)
+ puts bottles + " more bottle of beer on the wall, "
+ puts bottles + " more bottle of beer.\n"
+ end
+
+ puts "Take one down and pass it around, "
+
+ bottles = bottles - 1
+
+ if (bottles > 1)
+ puts bottles + " bottles of beer on the wall.\n"
+ end
+
+ if (bottles == 1)
+ puts bottles + " more bottle of beer on the wall.\n"
+ end
+
+ if (bottles == 0)
+ puts "no more bottles of beer on the wall.\n"
+ end
+
+ puts "\n"
+
+end
+
+puts
+"No more bottles of beer on the wall, no more bottles of beer.
+Go to the store and buy some more, 99 bottles of beer on the wall.\n"
View
@@ -15,6 +15,7 @@ extern int yylineno;
uint32_t qualifier_builder;
HklList* stmt_stack;
+HklList* array_stack;
int yywrap()
{
@@ -48,6 +49,7 @@ int main(int argc, const char* argv[])
hklr_init();
stmt_stack = hkl_list_new();
+ array_stack = hkl_list_new();
yyparse();
View
@@ -32,6 +32,10 @@ HklValue* hkl_value_new(HklType type, ...)
value->as.object = va_arg(argp, HklrObject*);
break;
+ case HKL_TYPE_ARRAY:
+ value->as.deque = va_arg(argp, HklDeque*);
+ break;
+
default:
break;
}
View
@@ -6,6 +6,7 @@
#include "hkl_string.h"
#include "hkl_hash.h"
#include "hklr_object.h"
+#include "hkl_deque.h"
typedef struct HklValue
{
@@ -19,7 +20,8 @@ typedef struct HklValue
double real;
HklString* string;
HklHash* hash;
- struct HklrObject* object;
+ HklDeque* deque;
+ HklrObject* object;
} as;
View
@@ -47,6 +47,10 @@ HklrExpression* hklr_expression_new(HklExpressionType type, ...)
expr->arg[0].string = va_arg(argp, HklString*);
break;
+ case HKL_EXPR_ARRAY:
+ expr->arg[0].list = va_arg(argp, HklList*);
+ break;
+
case HKL_EXPR_UNARY:
expr->arg[0].op = va_arg(argp, HklOperatorType);
expr->arg[1].expression = va_arg(argp, HklrExpression*);
@@ -67,6 +71,13 @@ HklrExpression* hklr_expression_new(HklExpressionType type, ...)
return expr;
}
+static void hklr_array_add_list(void* expr, void* array)
+{
+ HklValue* value = hklr_expression_eval((HklrExpression*) expr);
+
+ hkl_deque_push_back((HklDeque*) array, value);
+}
+
HklValue* hklr_expression_eval(HklrExpression* expr)
{
assert(expr != NULL);
@@ -105,6 +116,24 @@ HklValue* hklr_expression_eval(HklrExpression* expr)
}
break;
+ case HKL_EXPR_ID:
+ return hkl_value_new(HKL_TYPE_REF, hklr_search(expr->arg[0].string));
+ break;
+
+ case HKL_EXPR_ARRAY:
+ {
+ HklList* list = expr->arg[0].list;
+
+ // allocate space ahead of time
+ HklDeque* deque = hkl_deque_new_sized(list->size);
+ HklValue* result = hkl_value_new(HKL_TYPE_ARRAY, deque);
+
+ hkl_list_traverse(list, hklr_array_add_list, deque);
+
+ return result;
+ }
+ break;
+
case HKL_EXPR_UNARY:
{
HklValue* value = hklr_expression_eval(expr->arg[1].expression);
@@ -192,10 +221,6 @@ HklValue* hklr_expression_eval(HklrExpression* expr)
}
break; // HKL_EXPR_BINARY
- case HKL_EXPR_ID:
- return hkl_value_new(HKL_TYPE_REF, hklr_search(expr->arg[0].string));
- break;
-
default:
assert(false);
break;
View
@@ -5,6 +5,7 @@
#include "hkl_string.h"
#include "hkl_value.h"
+#include "hkl_list.h"
typedef enum HklExpressionType
{
@@ -17,7 +18,8 @@ typedef enum HklExpressionType
HKL_EXPR_GETS,
HKL_EXPR_UNARY,
HKL_EXPR_BINARY,
- HKL_EXPR_ID
+ HKL_EXPR_ID,
+ HKL_EXPR_ARRAY
} HklExpressionType;
@@ -62,6 +64,7 @@ typedef struct HklrExpression
HklString* string;
struct HklrExpression* expression;
HklOperatorType op;
+ HklList* list;
} arg[3];
@@ -34,6 +34,23 @@ void hklr_statement_puts(HklrExpression* expr)
fprintf(stdout, "%s", value->as.string->utf8_data);
break;
+ case HKL_TYPE_ARRAY:
+ {
+ HklDeque* deque = value->as.deque;
+ size_t i;
+ fprintf(stdout, "[");
+ for (i = 0; i < value->as.deque->size - 1; ++i)
+ {
+ fprintf(stdout, "%i, ", ((HklValue*) hkl_deque_findn(deque, i))->as.integer);
+ }
+ if (i < value->as.deque->size)
+ {
+ fprintf(stdout, "%i", ((HklValue*) hkl_deque_findn(deque, i))->as.integer);
+ }
+ fprintf(stdout, "]");
+ }
+ break;
+
default:
assert(false);
break;
View
@@ -12,10 +12,10 @@
// These will be defined at link time
extern int yylex();
extern int yyerror(char const*);
- extern HklList* array_builder;
extern uint32_t qualifier_builder;
extern HklList* stmt_stack;
+ extern HklList* array_stack;
%}
// Verbose Errors
@@ -135,7 +135,7 @@
%type <statement> if_stmt
%type <statement> while_stmt
-%type <list> expr_list
+%type <list> array
%type <expression> expr
%type <expression> optional_init
@@ -474,6 +474,9 @@ primary_expr:
| variable
| hash
| array
+ {
+ $$ = hklr_expression_new(HKL_EXPR_ARRAY, $1);
+ }
| inline_function
| inline_class
@@ -503,7 +506,7 @@ variable:
}
| variable HKL_T_LPAREN expr_list HKL_T_RPAREN
{
- $$ = hklr_expression_new(HKL_EXPR_BINARY, $1, HKL_OP_CALL, $3);
+ //$$ = hklr_expression_new(HKL_EXPR_BINARY, $1, HKL_OP_CALL, $3);
}
nocall_variable:
@@ -540,10 +543,17 @@ optional_value:
| empty
array:
- HKL_T_LBRACKET expr_list HKL_T_RBRACKET
+ HKL_T_LBRACKET { hkl_list_push_back(array_stack, hkl_list_new()); } expr_list HKL_T_RBRACKET
+ {
+ $$ = hkl_list_pop_back(array_stack);
+ }
expr_list:
expr expr_more
+ {
+ // add the statement to the current expr_list
+ hkl_list_push_back((HklList*) array_stack->tail->data, $1);
+ }
| empty
expr_more:

0 comments on commit 53c6bb7

Please sign in to comment.