Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
puts only works for literals right now. gets does not exactly work yet.
- Loading branch information
Showing
11 changed files
with
209 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
puts gets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
puts "Hello World!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,100 @@ | ||
#include <stdio.h> | ||
#include <assert.h> | ||
|
||
#include "hkl_alloc.h" | ||
#include "hkl_expression.h" | ||
|
||
HklExpression* hkl_expression_new(HklExpressionType type, ...) | ||
{ | ||
HklExpression* expr = hkl_alloc_object(HklExpression); | ||
|
||
expr->type = type; | ||
|
||
va_list argp; | ||
va_start(argp, type); | ||
|
||
switch (type) | ||
{ | ||
case HKL_EXPR_INT: | ||
expr->arg[0].integer = va_arg(argp, int); | ||
break; | ||
|
||
case HKL_EXPR_REAL: | ||
expr->arg[0].real = va_arg(argp, double); | ||
break; | ||
|
||
case HKL_EXPR_STRING: | ||
expr->arg[0].string = va_arg(argp, HklString*); | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
|
||
va_end(argp); | ||
|
||
return expr; | ||
} | ||
|
||
HklString* hkl_expression_eval_string(HklExpression* expr) | ||
{ | ||
assert(expr != NULL); | ||
|
||
switch (expr->type) | ||
{ | ||
case HKL_EXPR_STRING: | ||
// return a copy of the string | ||
return hkl_string_new_from_string(expr->arg[0].string); | ||
break; | ||
|
||
case HKL_EXPR_INT: | ||
return hkl_string_new_from_integer(expr->arg[0].integer); | ||
break; | ||
|
||
case HKL_EXPR_REAL: | ||
return hkl_string_new_from_real(expr->arg[0].real); | ||
break; | ||
|
||
case HKL_EXPR_GETS: | ||
{ | ||
HklString* buffer = hkl_string_new(); | ||
uint32_t c; | ||
|
||
while (true) { | ||
|
||
c = fgetc(stdin); | ||
if (c == (uint32_t) EOF || c == '\n') | ||
break; | ||
|
||
hkl_string_cat_character(buffer, c); | ||
} | ||
|
||
return buffer; | ||
} | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
|
||
assert(false); | ||
return NULL; | ||
} | ||
|
||
void hkl_expression_free(HklExpression* expr) | ||
{ | ||
assert(expr != NULL); | ||
|
||
switch (expr->type) | ||
{ | ||
case HKL_EXPR_STRING: | ||
// free the internal string | ||
hkl_string_free(expr->arg[0].string); | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
|
||
hkl_free_object(expr); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters