solve task 2, day 5

This commit is contained in:
Felix Suchert 2022-12-28 02:57:47 +01:00
parent a1385956ce
commit c1f073246a
Signed by: feliix42
GPG key ID: 24363525EA0E8A99

View file

@ -6,6 +6,8 @@
// Code for the header file generated by bison // Code for the header file generated by bison
%code requires { %code requires {
#include <stdbool.h>
typedef void* yyscan_t; typedef void* yyscan_t;
#define NUM_PILES 9 #define NUM_PILES 9
@ -20,6 +22,8 @@
// points to the topmost item in the stack // points to the topmost item in the stack
struct stack_item** stacks; struct stack_item** stacks;
unsigned stack_idx; unsigned stack_idx;
// whether the crane is generation two (task 2)
bool is_gen_two;
}; };
} }
@ -27,6 +31,7 @@
%{ %{
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h>
#include "parser.h" #include "parser.h"
#include "lexer.h" #include "lexer.h"
@ -115,32 +120,58 @@ expr
: MOVE_OP NUMBER ORIGIN NUMBER DEST NUMBER { : MOVE_OP NUMBER ORIGIN NUMBER DEST NUMBER {
// Source: $4, Dst: $6, repeat $2 times // Source: $4, Dst: $6, repeat $2 times
// printf("move %lu from %lu to %lu", $2, $4, $6); // printf("move %lu from %lu to %lu", $2, $4, $6);
for (unsigned long i = 0; i < $2; i++) {
// get the current element
struct stack_item* it = state->stacks[$4 -1];
if (!it) { if (state->is_gen_two) {
// TODO(feliix42): use yyerror? struct stack_item* it = state->stacks[$4 - 1];
fprintf(stderr, "\033[91m[parser error] tried to pop from empty stack\n\033[0m"); for (unsigned long i = 1; i < $2; i++) {
break; if (!it) {
// TODO(feliix42): use yyerror?
fprintf(stderr, "\033[91m[parser error] tried to pop from empty stack\n\033[0m");
break;
}
it = it->next;
} }
// delete old references struct stack_item* tmp = state->stacks[$4 - 1];
state->stacks[$4 -1] = it->next; state->stacks[$4 - 1] = it->next;
// insert on new stack it->next = state->stacks[$6 - 1];
it->next = state->stacks[$6 -1]; state->stacks[$6 - 1] = tmp;
state->stacks[$6 -1] = it; } else {
for (unsigned long i = 0; i < $2; i++) {
// get the current element
struct stack_item* it = state->stacks[$4 -1];
if (!it) {
// TODO(feliix42): use yyerror?
fprintf(stderr, "\033[91m[parser error] tried to pop from empty stack\n\033[0m");
break;
}
// delete old references
state->stacks[$4 -1] = it->next;
// insert on new stack
it->next = state->stacks[$6 -1];
state->stacks[$6 -1] = it;
}
} }
} }
; ;
%% %%
int main(void) { int main(int argc, char** argv) {
struct parser_state parser_state; struct parser_state parser_state;
parser_state.stack_idx = 0; parser_state.stack_idx = 0;
if (argc > 1 && argv[1][0] == '2') {
parser_state.is_gen_two = true;
} else {
parser_state.is_gen_two = false;
}
parser_state.stacks = calloc(NUM_PILES, sizeof(struct stack_item*)); parser_state.stacks = calloc(NUM_PILES, sizeof(struct stack_item*));
if (!parser_state.stacks) { if (!parser_state.stacks) {
fprintf(stderr, "\033[91m[error] Could not initialize parser state\033[0m\n"); fprintf(stderr, "\033[91m[error] Could not initialize parser state\033[0m\n");