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 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,6 +120,25 @@ 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);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct stack_item* tmp = state->stacks[$4 - 1];
|
||||||
|
state->stacks[$4 - 1] = it->next;
|
||||||
|
|
||||||
|
it->next = state->stacks[$6 - 1];
|
||||||
|
state->stacks[$6 - 1] = tmp;
|
||||||
|
} else {
|
||||||
for (unsigned long i = 0; i < $2; i++) {
|
for (unsigned long i = 0; i < $2; i++) {
|
||||||
// get the current element
|
// get the current element
|
||||||
struct stack_item* it = state->stacks[$4 -1];
|
struct stack_item* it = state->stacks[$4 -1];
|
||||||
|
@ -133,14 +157,21 @@ expr
|
||||||
state->stacks[$6 -1] = it;
|
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");
|
||||||
|
|
Loading…
Reference in a new issue