codebased/scirpt/main.c
2023-04-13 02:29:49 +02:00

57 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 = "123 if +";
printf("test program = \"\"\"\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_expr(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);
}
}