solve task 2, day 5
This commit is contained in:
parent
a1385956ce
commit
c1f073246a
1 changed files with 44 additions and 13 deletions
|
@ -6,6 +6,8 @@
|
|||
|
||||
// Code for the header file generated by bison
|
||||
%code requires {
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef void* yyscan_t;
|
||||
|
||||
#define NUM_PILES 9
|
||||
|
@ -20,6 +22,8 @@
|
|||
// points to the topmost item in the stack
|
||||
struct stack_item** stacks;
|
||||
unsigned stack_idx;
|
||||
// whether the crane is generation two (task 2)
|
||||
bool is_gen_two;
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -27,6 +31,7 @@
|
|||
%{
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include "parser.h"
|
||||
#include "lexer.h"
|
||||
|
||||
|
@ -115,32 +120,58 @@ expr
|
|||
: MOVE_OP NUMBER ORIGIN NUMBER DEST NUMBER {
|
||||
// Source: $4, Dst: $6, repeat $2 times
|
||||
// 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) {
|
||||
// TODO(feliix42): use yyerror?
|
||||
fprintf(stderr, "\033[91m[parser error] tried to pop from empty stack\n\033[0m");
|
||||
break;
|
||||
if (state->is_gen_two) {
|
||||
struct stack_item* it = state->stacks[$4 - 1];
|
||||
for (unsigned long i = 1; i < $2; i++) {
|
||||
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
|
||||
state->stacks[$4 -1] = it->next;
|
||||
struct stack_item* tmp = state->stacks[$4 - 1];
|
||||
state->stacks[$4 - 1] = it->next;
|
||||
|
||||
// insert on new stack
|
||||
it->next = state->stacks[$6 -1];
|
||||
state->stacks[$6 -1] = it;
|
||||
it->next = state->stacks[$6 - 1];
|
||||
state->stacks[$6 - 1] = tmp;
|
||||
} 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;
|
||||
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*));
|
||||
if (!parser_state.stacks) {
|
||||
fprintf(stderr, "\033[91m[error] Could not initialize parser state\033[0m\n");
|
||||
|
|
Loading…
Reference in a new issue