codebased/scirpt/main.c
2023-04-17 02:25:26 +02:00

58 lines
1.4 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 <string.h>
int main(void)
{
const char* text
= "if 123 { \"abc\"; abc()() }; for a in b { print(123, 123, 123,) }";
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) {
ScirptAstExpr* expr = scirpt_parser_next(parser);
if (expr->type == ScirptAstExprTypeEof) {
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_ast_expr_delete(expr);
}
scirpt_parser_delete(parser);
}
}