Adds support for TEXT type. Is passed as const char * to C/C++.

This commit is contained in:
Felix Queissner 2015-08-20 17:51:31 +02:00
parent c057f3dbbf
commit 81ad6c3406
5 changed files with 36 additions and 7 deletions

View file

@ -12,7 +12,6 @@ Also it leaks memory. A lot of memory.
- Fixing Memory Leaks
- Adding support for Modules
- Adding support for Pointer Types
- Adding support for String Type
## Guidlines
- Calls to `die` or `die_extra` should follow the following scheme: `ContextName.ErrorName`

View file

@ -4,9 +4,11 @@ VAR global : INT;
# OBJ heap : "/sys/malloc";
# OBJ interrupts : "/sys/interrupt";
PUB main() | i : INT
PUB main() | i : INT, str : TEXT
BEGIN
0 -> i;
"Hello " -> str;
printStr(str + "World!");
WHILE ((i + 1) -> i) <= fun() DO
BEGIN
hlp(i, fun() - i);

View file

@ -85,7 +85,12 @@ Variable NativeMethod::invoke(Vector<Variable> arguments)
if(arguments[i].type() != this->parameters[i]) {
die_extra("NativeMethod.InvalidArgumentType", arguments[i].type().name());
}
stackSize += arguments[i].type().size();
// Special case TEXT: Copy const char * instead of object
if(arguments[i].type() == Type::Text) {
stackSize += sizeof(const char *);
} else {
stackSize += arguments[i].type().size();
}
}
uint8_t *stack = (uint8_t*)malloc(stackSize);
@ -95,9 +100,18 @@ Variable NativeMethod::invoke(Vector<Variable> arguments)
size_t size = arguments[i].type().size();
void *data = arguments[i].data();
if(size > 0) {
memcpy(stackPtr, data, size);
stackPtr += size;
if(arguments[i].type() == Type::Text) {
// Copy const char *
const char *text = arguments[i].value<Text>();
memcpy(stackPtr, &text, sizeof(const char *));
stackPtr += sizeof(const char *);
}
else {
if(size > 0) {
memcpy(stackPtr, data, size);
stackPtr += size;
}
}
}
@ -116,6 +130,10 @@ extern "C" void __cdecl print2Int(int a, int b) {
kprintf("{%d;%d}\n", a, b);
}
extern "C" void __cdecl printStr(const char *text) {
kprintf("{%s}", text);
}
struct NativeModuleDef
{
const char *name;
@ -128,6 +146,7 @@ NativeModuleDef methods[] = {
{ "timer_get", "", (void*)timer_get },
{ "timer_set", "i", (void*)timer_set },
{ "printInt", "i", (void*)printInt },
{ "printStr", "t", (void*)printStr },
{ "print2Int", "ii", (void*)print2Int },
{ nullptr, nullptr, 0 }
};

View file

@ -9,6 +9,9 @@
%option reentrant
%option bison-bridge
%option yylineno
%x strparse
%%
\#[^\n]* ; // Eat all the comments!
[ \t]+ ; // Eat all the whitespace!
@ -56,6 +59,11 @@ TO { return KW_TO; }
UNTIL { return KW_UNTIL; }
WHILE { return KW_WHILE; }
DO { return KW_DO; }
"\"" BEGIN(strparse);
<strparse>[^\"]* { yylval->text = yyextra->strdup(yytext); return TEXT; }
<strparse>"\"" BEGIN(INITIAL);
[0-9]+\.[0-9]* { yylval->fval = atof(yytext); return REAL; }
[0-9]+ { yylval->ival = atoi(yytext); return INT; }
[a-zA-Z0-9'_]+ { yylval->text = yyextra->strdup(yytext); return IDENTIFIER; }

View file

@ -66,6 +66,7 @@ void yyerror(void *scanner, const char *s);
%token <fval> REAL
%token <ival> INT
%token <text> IDENTIFIER
%token <text> TEXT
%token KW_PUB
%token KW_PRI
@ -319,7 +320,7 @@ elseIfLoop:
expression:
INT { $$ = new ConstantExpression(Variable::fromInt($1)); }
| REAL { $$ = new ConstantExpression(Variable::fromReal($1)); }
// | TEXT { $$ = new ConstantExpression(Variable::fromText($1)); }
| TEXT { $$ = new ConstantExpression(Variable::fromText($1)); }
| IDENTIFIER { $$ = new VariableExpression($1); }
| IDENTIFIER LBRACKET expressionList RBRACKET {
auto *call = new MethodInvokeExpression($1);