codebased/scirpt/main.c
2023-04-19 00:03:40 +02:00

62 lines
1.5 KiB
C

#include "scirpt/ast.h"
#include "scirpt/lexer.h"
#include "scirpt/parser.h"
#include "scirpt/token.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
const char* text
= "if 123 { \"abc\"; abc()() }; for a in b { print(123, "
"123, 123, [123, 123], !{ \"foo\": 123, bar: \"baz\" }) }";
printf("\nprogram = \"\"\"\n%s\n\"\"\"\n", text);
{
printf("\n- test lexer\n");
ScirptLexer* lexer = scirpt_lexer_new(text, strlen(text));
while (true) {
ScirptToken token = scirpt_lexer_next(lexer);
if (token.type == ScirptTokenTypeEof)
break;
printf("%d\n", token.type);
}
scirpt_lexer_delete(lexer);
}
{
printf("\n- test lexer + parser\n");
ScirptLexer* lexer = scirpt_lexer_new(text, strlen(text));
ScirptParser* parser = scirpt_parser_new(text, strlen(text), lexer);
while (true) {
ScirptExpr* expr = scirpt_parser_next(parser);
if (expr->type == ScirptExprTypeEof) {
break;
} else if (!scirpt_parser_ok(parser)) {
const ScirptParserErrorArray* errors
= scirpt_parser_errors(parser);
for (size_t i = 0; i < scirpt_parser_error_array_length(errors);
++i) {
ScirptParserError* error
= scirpt_parser_error_array_get(errors, i);
printf(
"error: %s at %d:%d\n",
error->message.data,
error->pos.line,
error->pos.col
);
}
break;
}
printf("%d\n", expr->type);
scirpt_expr_delete(expr);
}
scirpt_parser_delete(parser);
}
return EXIT_SUCCESS;
}