From ef2f4247a73741f7d6ed668a89c77a759bac003e Mon Sep 17 00:00:00 2001 From: Colin Holzman Date: Tue, 23 Mar 2021 08:47:00 -0400 Subject: [PATCH] Minor refactoring. --- example/oven.c | 79 ++++++++++++------- example/oven.h | 4 +- .../xyz/colinholzman/makina/CodeGenerator.kt | 6 +- 3 files changed, 55 insertions(+), 34 deletions(-) diff --git a/example/oven.c b/example/oven.c index acf27b6..5c85a93 100644 --- a/example/oven.c +++ b/example/oven.c @@ -8,47 +8,68 @@ static int oven_open(struct oven *, struct oven_event *); static int oven_closed_idle(struct oven *self, struct oven_event *event) { if (!self || !event) return -1; - if (event->id == oven_event_start && 1) { - self->state = NULL; - self->state = oven_closed_cooking; - enable_heater(self, event); - return 0; - } - if (event->id == oven_event_open && 1) { - self->state = NULL; - self->state = oven_open; - return 0; + switch (event->id) { + case oven_event_start: + if (1) { + self->state = NULL; + self->state = oven_closed_cooking; + enable_heater(self, event); + break; + } + break; + case oven_event_open: + if (1) { + self->state = NULL; + self->state = oven_open; + break; + } + break; + default: break; } return 0; } static int oven_closed_cooking(struct oven *self, struct oven_event *event) { if (!self || !event) return -1; - if (event->id == oven_event_timeout && 1) { - disable_heater(self, event); - self->state = NULL; - self->state = oven_closed_idle; - return 0; - } - if (event->id == oven_event_open && 1) { - disable_heater(self, event); - self->state = NULL; - self->state = oven_open; - return 0; + switch (event->id) { + case oven_event_timeout: + if (1) { + disable_heater(self, event); + self->state = NULL; + self->state = oven_closed_idle; + break; + } + break; + case oven_event_open: + if (1) { + disable_heater(self, event); + self->state = NULL; + self->state = oven_open; + break; + } + break; + default: break; } return 0; } static int oven_open(struct oven *self, struct oven_event *event) { if (!self || !event) return -1; - if (event->id == oven_event_close && 1) { - self->state = NULL; - self->state = oven_closed_idle; - return 0; - } - if (event->id == oven_event_start && 1) { - error(self, event); - return 0; + switch (event->id) { + case oven_event_close: + if (1) { + self->state = NULL; + self->state = oven_closed_idle; + break; + } + break; + case oven_event_start: + if (1) { + error(self, event); + break; + } + break; + default: break; } return 0; } diff --git a/example/oven.h b/example/oven.h index 57a3e16..9f04e9b 100644 --- a/example/oven.h +++ b/example/oven.h @@ -17,13 +17,13 @@ enum oven_event_id { }; struct oven_event { + void * ctx; enum oven_event_id id; - void *ctx; }; struct oven { int (*state)(struct oven *, struct oven_event *); - void *ctx; + void * ctx; }; int oven_init(struct oven *); diff --git a/makina-compiler/src/xyz/colinholzman/makina/CodeGenerator.kt b/makina-compiler/src/xyz/colinholzman/makina/CodeGenerator.kt index b3a2d31..7a49b2c 100644 --- a/makina-compiler/src/xyz/colinholzman/makina/CodeGenerator.kt +++ b/makina-compiler/src/xyz/colinholzman/makina/CodeGenerator.kt @@ -17,8 +17,8 @@ class CodeGenerator(val machine: Machine, println("/*This file was generated by Makina. Do not modify.*/") println("#ifndef ${machine.id.toUpperCase()}_H") println("#define ${machine.id.toUpperCase()}_H") - println() for (include in includes) { + println() println("#include \"$include\"") } println() @@ -105,8 +105,8 @@ class CodeGenerator(val machine: Machine, println("\tif (!self || !event) return -1;") val config = activeLeafState.getStateConfiguration() println("\tswitch (event->id) {") - val handlerStatePairs = config.getHandlers().groupByIdAndRemoveRedundantHandlers() - for (entry in handlerStatePairs) { + val handlerGroups = config.getHandlers().groupByIdAndRemoveRedundantHandlers() + for (entry in handlerGroups) { val eventId = entry.key println("\tcase ${machine.id}_event_$eventId:") for (handlerStatePair in entry.value) {