This commit is contained in:
cozis
2022-01-22 02:06:24 +01:00
parent 7f29b7c01a
commit 3df0cc0a7d
3 changed files with 37 additions and 7 deletions
+9 -7
View File
@@ -171,10 +171,6 @@ static Object *walkListValue(const char *fmt, int *i, va_list va, Heap *heap, Er
static Object *walkValue(const char *fmt, int *i, va_list va, Heap *heap, Error *error) static Object *walkValue(const char *fmt, int *i, va_list va, Heap *heap, Error *error)
{ {
int i_;
if(i == NULL)
i = &i_;
while(isspace(fmt[*i])) while(isspace(fmt[*i]))
*i += 1; *i += 1;
@@ -222,16 +218,21 @@ static Object *walkValue(const char *fmt, int *i, va_list va, Heap *heap, Error
if(fmt[*i] == '\0') if(fmt[*i] == '\0')
{ {
// ERROR: Format ended inside of ${..}. // ERROR: Format ended inside of ${..}.
Error_Report(error, 0, "Format ended inside ${..}");
return NULL; return NULL;
} }
int end = fmt[*i]; assert(fmt[*i] == '}');
int end = *i;
// NOTE: The last '}' is skipped after the switch. // NOTE: The last '}' is skipped after the switch.
const char *src = fmt + start; const char *src = fmt + start;
int len = end - start; int len = end - start;
printf("src: [%.*s]\n", len, src);
o = eval(src, len, NULL, heap, error); o = eval(src, len, NULL, heap, error);
if(o == NULL) if(o == NULL)
@@ -281,7 +282,8 @@ static Object *walkValue(const char *fmt, int *i, va_list va, Heap *heap, Error
Object *buildValue2(Heap *heap, Error *error, const char *fmt, va_list va) Object *buildValue2(Heap *heap, Error *error, const char *fmt, va_list va)
{ {
return walkValue(fmt, NULL, va, heap, error); int i = 0;
return walkValue(fmt, &i, va, heap, error);
} }
Object *buildValue(Heap *heap, Error *error, const char *fmt, ...) Object *buildValue(Heap *heap, Error *error, const char *fmt, ...)
+4
View File
@@ -1,3 +1,4 @@
#include <string.h>
#include "compiler/parse.h" #include "compiler/parse.h"
#include "compiler/compile.h" #include "compiler/compile.h"
#include "runtime/runtime.h" #include "runtime/runtime.h"
@@ -6,6 +7,9 @@
Object *eval(const char *str, int len, Object *closure, Heap *heap, Error *error) Object *eval(const char *str, int len, Object *closure, Heap *heap, Error *error)
{ {
if(len < 0)
len = strlen(str);
Source *s = Source_FromString(NULL, str, len, error); Source *s = Source_FromString(NULL, str, len, error);
if(s == NULL) if(s == NULL)
+24
View File
@@ -103,11 +103,35 @@ static int parse_args(Options *opts, int argc, char **argv, Error *error)
return i; return i;
} }
#include "buildvalue.h"
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
Error error; Error error;
Error_Init(&error); Error_Init(&error);
{
Heap *heap = Heap_New(-1);
if(heap == NULL)
{
fprintf(stderr, "ERROR: Failed to create heap.\n");
return -1;
}
Object *o = buildValue(heap, &error, "${ return 3+1; }");
if(o == NULL)
{
fprintf(stderr, "ERROR: %s.\n", error.message);
return -1;
}
Object_Print(o, stdout);
Heap_Free(heap);
}
// Parse command line options. // Parse command line options.