Add AST generation - #22

This commit is contained in:
josh committed 2024-04-22 20:51:27 -04:00
1 parent 2b28ef622d
commit cb06a56f81
11 files changed
+531 -30

No files matched your search

+42
View File
@@ -803,6 +803,48 @@ Opt -> ;
EOF
run_propane(language: language)
end
it "generates an AST" do
write_grammar <<EOF
ast;
ptype int;
token a << $$ = 11; >>
token b << $$ = 22; >>
token one /1/;
token two /2/;
token comma /,/ <<
$$ = 42;
>>
token lparen /\\(/;
token rparen /\\)/;
drop /\\s+/;
Start -> Items;
Items -> Item ItemsMore;
Items -> ;
ItemsMore -> comma Item ItemsMore;
ItemsMore -> ;
Item -> a;
Item -> b;
Item -> lparen Item rparen;
Item -> Dual;
Dual -> One Two;
Dual -> Two One;
One -> one;
Two -> two;
EOF
run_propane(language: language)
compile("spec/test_ast.#{language}", language: language)
results = run_test
expect(results.stderr).to eq ""
expect(results.status).to eq 0
end
end
end
end
+54
View File
@@ -0,0 +1,54 @@
#include "testparser.h"
#include <assert.h>
#include <string.h>
#include "testutils.h"
int main()
{
char const * input = "a, ((b)), b";
p_context_t context;
p_context_init(&context, (uint8_t const *)input, strlen(input));
assert_eq(P_SUCCESS, p_parse(&context));
Start * start = p_result(&context);
assert(start->pItems1 != NULL);
Items * items = start->pItems1;
assert(items->pItem1 != NULL);
assert(items->pItem1->pToken1 != NULL);
assert_eq(TOKEN_a, items->pItem1->pToken1->token);
assert_eq(11, items->pItem1->pToken1->pvalue);
assert(items->pItemsMore2 != NULL);
ItemsMore * itemsmore = items->pItemsMore2;
assert(itemsmore->pItem2 != NULL);
assert(itemsmore->pItem2->pItem2 != NULL);
assert(itemsmore->pItem2->pItem2->pItem2 != NULL);
assert(itemsmore->pItem2->pItem2->pItem2->pToken1 != NULL);
assert_eq(TOKEN_b, itemsmore->pItem2->pItem2->pItem2->pToken1->token);
assert_eq(22, itemsmore->pItem2->pItem2->pItem2->pToken1->pvalue);
assert(itemsmore->pItemsMore3 != NULL);
itemsmore = itemsmore->pItemsMore3;
assert(itemsmore->pItem2 != NULL);
assert(itemsmore->pItem2->pToken1 != NULL);
assert_eq(TOKEN_b, itemsmore->pItem2->pToken1->token);
assert_eq(22, itemsmore->pItem2->pToken1->pvalue);
assert(itemsmore->pItemsMore3 == NULL);
input = "";
p_context_init(&context, (uint8_t const *)input, strlen(input));
assert_eq(P_SUCCESS, p_parse(&context));
start = p_result(&context);
assert(start->pItems1 == NULL);
input = "2 1";
p_context_init(&context, (uint8_t const *)input, strlen(input));
assert_eq(P_SUCCESS, p_parse(&context));
start = p_result(&context);
assert(start->pItems1 != NULL);
assert(start->pItems1->pItem1 != NULL);
assert(start->pItems1->pItem1->pDual1 != NULL);
assert(start->pItems1->pItem1->pDual1->pTwo1 != NULL);
assert(start->pItems1->pItem1->pDual1->pOne2 != NULL);
assert(start->pItems1->pItem1->pDual1->pTwo2 == NULL);
assert(start->pItems1->pItem1->pDual1->pOne1 == NULL);
return 0;
}
+56
View File
@@ -0,0 +1,56 @@
import testparser;
import std.stdio;
import testutils;
int main()
{
return 0;
}
unittest
{
string input = "a, ((b)), b";
p_context_t context;
p_context_init(&context, input);
assert_eq(P_SUCCESS, p_parse(&context));
Start * start = p_result(&context);
assert(start.pItems1 !is null);
Items * items = start.pItems1;
assert(items.pItem1 !is null);
assert(items.pItem1.pToken1 !is null);
assert_eq(TOKEN_a, items.pItem1.pToken1.token);
assert_eq(11, items.pItem1.pToken1.pvalue);
assert(items.pItemsMore2 !is null);
ItemsMore * itemsmore = items.pItemsMore2;
assert(itemsmore.pItem2 !is null);
assert(itemsmore.pItem2.pItem2 !is null);
assert(itemsmore.pItem2.pItem2.pItem2 !is null);
assert(itemsmore.pItem2.pItem2.pItem2.pToken1 !is null);
assert_eq(TOKEN_b, itemsmore.pItem2.pItem2.pItem2.pToken1.token);
assert_eq(22, itemsmore.pItem2.pItem2.pItem2.pToken1.pvalue);
assert(itemsmore.pItemsMore3 !is null);
itemsmore = itemsmore.pItemsMore3;
assert(itemsmore.pItem2 !is null);
assert(itemsmore.pItem2.pToken1 !is null);
assert_eq(TOKEN_b, itemsmore.pItem2.pToken1.token);
assert_eq(22, itemsmore.pItem2.pToken1.pvalue);
assert(itemsmore.pItemsMore3 is null);
input = "";
p_context_init(&context, input);
assert_eq(P_SUCCESS, p_parse(&context));
start = p_result(&context);
assert(start.pItems1 is null);
input = "2 1";
p_context_init(&context, input);
assert_eq(P_SUCCESS, p_parse(&context));
start = p_result(&context);
assert(start.pItems1 !is null);
assert(start.pItems1.pItem1 !is null);
assert(start.pItems1.pItem1.pDual1 !is null);
assert(start.pItems1.pItem1.pDual1.pTwo1 !is null);
assert(start.pItems1.pItem1.pDual1.pOne2 !is null);
assert(start.pItems1.pItem1.pDual1.pTwo2 is null);
assert(start.pItems1.pItem1.pDual1.pOne1 is null);
}