implementing string and char stacks

This commit is contained in:
Rowan Torbitzky-Lane 2025-01-24 01:38:14 -06:00
parent 44257bd94e
commit 452e7a2b49
4 changed files with 33 additions and 4 deletions

View File

@ -44,6 +44,7 @@ library
, Instructions.GenericInstructions , Instructions.GenericInstructions
, Instructions.LogicalInstructions , Instructions.LogicalInstructions
, Instructions.CodeInstructions , Instructions.CodeInstructions
, Instructions.StringInstructions
-- Modules included in this library but not exported. -- Modules included in this library but not exported.
-- other-modules: -- other-modules:

View File

@ -6,10 +6,6 @@ the functions in the pyshgp list.
https://erp12.github.io/pyshgp/html/core_instructions.html https://erp12.github.io/pyshgp/html/core_instructions.html
I'm not going to include no-ops.
Interpush doesn't have the code stack. Time to test this against pysh.
## Tasks ## Tasks
* [ ] Post minimal core of exec to haskell discourse for advice about speed optimization. * [ ] Post minimal core of exec to haskell discourse for advice about speed optimization.
* [x] Do test-driven development on this one. * [x] Do test-driven development on this one.

View File

@ -23,9 +23,13 @@ instructionParameterLoad state@(State {_parameter = (p : _)}) = case p of
(GeneInt val) -> state & int .~ val : view int state (GeneInt val) -> state & int .~ val : view int state
(GeneFloat val) -> state & float .~ val : view float state (GeneFloat val) -> state & float .~ val : view float state
(GeneBool val) -> state & bool .~ val : view bool state (GeneBool val) -> state & bool .~ val : view bool state
(GeneString val) -> state & string .~ val : view string state
(GeneChar val) -> state & char .~ val : view char state
(GeneIntVector val) -> state & intVector .~ val : view intVector state (GeneIntVector val) -> state & intVector .~ val : view intVector state
(GeneFloatVector val) -> state & floatVector .~ val : view floatVector state (GeneFloatVector val) -> state & floatVector .~ val : view floatVector state
(GeneBoolVector val) -> state & boolVector .~ val : view boolVector state (GeneBoolVector val) -> state & boolVector .~ val : view boolVector state
(GeneStringVector val) -> state & stringVector .~ val : view stringVector state
(GeneCharVector val) -> state & charVector .~ val : view charVector state
(StateFunc _) -> undefined (StateFunc _) -> undefined
(PlaceInput _) -> undefined (PlaceInput _) -> undefined
Close -> undefined Close -> undefined
@ -53,9 +57,13 @@ interpretExec state@(State {_exec = (e : es)}) =
(GeneInt val) -> interpretExec (state & exec .~ es & int .~ val : view int state) (GeneInt val) -> interpretExec (state & exec .~ es & int .~ val : view int state)
(GeneFloat val) -> interpretExec (state & exec .~ es & float .~ val : view float state) (GeneFloat val) -> interpretExec (state & exec .~ es & float .~ val : view float state)
(GeneBool val) -> interpretExec (state & exec .~ es & bool .~ val : view bool state) (GeneBool val) -> interpretExec (state & exec .~ es & bool .~ val : view bool state)
(GeneString val) -> interpretExec (state & exec .~ es & string .~ val : view string state)
(GeneChar val) -> interpretExec (state & exec .~ es & char .~ val : view char state)
(GeneIntVector val) -> interpretExec (state & exec .~ es & intVector .~ val : view intVector state) (GeneIntVector val) -> interpretExec (state & exec .~ es & intVector .~ val : view intVector state)
(GeneFloatVector val) -> interpretExec (state & exec .~ es & floatVector .~ val : view floatVector state) (GeneFloatVector val) -> interpretExec (state & exec .~ es & floatVector .~ val : view floatVector state)
(GeneBoolVector val) -> interpretExec (state & exec .~ es & boolVector .~ val : view boolVector state) (GeneBoolVector val) -> interpretExec (state & exec .~ es & boolVector .~ val : view boolVector state)
(GeneStringVector val) -> interpretExec (state & exec .~ es & stringVector .~ val : view stringVector state)
(GeneCharVector val) -> interpretExec (state & exec .~ es & charVector .~ val : view charVector state)
(StateFunc func) -> interpretExec $ func state {_exec = es} (StateFunc func) -> interpretExec $ func state {_exec = es}
(Block block) -> interpretExec (state {_exec = block ++ es}) (Block block) -> interpretExec (state {_exec = block ++ es})
(PlaceInput val) -> interpretExec (state {_exec = (view input state Map.! val) : es}) (PlaceInput val) -> interpretExec (state {_exec = (view input state Map.! val) : es})

View File

@ -13,9 +13,13 @@ data Gene
= GeneInt Int = GeneInt Int
| GeneFloat Float | GeneFloat Float
| GeneBool Bool | GeneBool Bool
| GeneString String
| GeneChar Char
| GeneIntVector [Int] | GeneIntVector [Int]
| GeneFloatVector [Float] | GeneFloatVector [Float]
| GeneBoolVector [Bool] | GeneBoolVector [Bool]
| GeneStringVector [String]
| GeneCharVector [Char]
| StateFunc (State -> State) | StateFunc (State -> State)
| PlaceInput String | PlaceInput String
| Close | Close
@ -25,10 +29,14 @@ instance Eq Gene where
GeneInt x == GeneInt y = x == y GeneInt x == GeneInt y = x == y
GeneFloat x == GeneFloat y = x == y GeneFloat x == GeneFloat y = x == y
GeneBool x == GeneBool y = x == y GeneBool x == GeneBool y = x == y
GeneString x == GeneString y = x == y
GeneChar x == GeneChar y = x == y
PlaceInput x == PlaceInput y = x == y PlaceInput x == PlaceInput y = x == y
GeneIntVector xs == GeneIntVector ys = xs == ys GeneIntVector xs == GeneIntVector ys = xs == ys
GeneFloatVector xs == GeneFloatVector ys = xs == ys GeneFloatVector xs == GeneFloatVector ys = xs == ys
GeneBoolVector xs == GeneBoolVector ys = xs == ys GeneBoolVector xs == GeneBoolVector ys = xs == ys
GeneStringVector xs == GeneStringVector ys = xs == ys
GeneCharVector xs == GeneCharVector ys = xs == ys
Close == Close = True Close == Close = True
StateFunc _ == StateFunc _ = True -- This line is probably not the best thing to do StateFunc _ == StateFunc _ = True -- This line is probably not the best thing to do
Block x == Block y = x == y Block x == Block y = x == y
@ -38,11 +46,15 @@ instance Show Gene where
show (GeneInt x) = "Int: " <> show x show (GeneInt x) = "Int: " <> show x
show (GeneFloat x) = "Float: " <> show x show (GeneFloat x) = "Float: " <> show x
show (GeneBool x) = "Bool: " <> show x show (GeneBool x) = "Bool: " <> show x
show (GeneString x) = "String: " <> x
show (GeneChar x) = "Char: " <> show x
show (StateFunc _) = "Func: unnamed" show (StateFunc _) = "Func: unnamed"
show (PlaceInput x) = "In: " <> x show (PlaceInput x) = "In: " <> x
show (GeneIntVector xs) = "Int Vec: " <> show xs show (GeneIntVector xs) = "Int Vec: " <> show xs
show (GeneFloatVector xs) = "Float Vec: " <> show xs show (GeneFloatVector xs) = "Float Vec: " <> show xs
show (GeneBoolVector xs) = "Bool Vec: " <> show xs show (GeneBoolVector xs) = "Bool Vec: " <> show xs
show (GeneStringVector xs) = "String Vec: " <> show xs
show (GeneCharVector xs) = "Char Vec: " <> show xs
show Close = "Close" show Close = "Close"
show (Block xs) = "Block: " <> show xs show (Block xs) = "Block: " <> show xs
@ -52,9 +64,13 @@ data State = State
_int :: [Int], _int :: [Int],
_float :: [Float], _float :: [Float],
_bool :: [Bool], _bool :: [Bool],
_string :: [String],
_char :: [Char],
_intVector :: [[Int]], _intVector :: [[Int]],
_floatVector :: [[Float]], _floatVector :: [[Float]],
_boolVector :: [[Bool]], _boolVector :: [[Bool]],
_stringVector :: [[String]],
_charVector :: [[Char]],
_parameter :: [Gene], _parameter :: [Gene],
_input :: Map.Map String Gene _input :: Map.Map String Gene
} }
@ -70,10 +86,14 @@ emptyState =
_int = [], _int = [],
_float = [], _float = [],
_bool = [], _bool = [],
_string = [],
_char = [],
_parameter = [], _parameter = [],
_intVector = [], _intVector = [],
_floatVector = [], _floatVector = [],
_boolVector = [], _boolVector = [],
_stringVector = [],
_charVector = [],
_input = Map.empty _input = Map.empty
} }
@ -85,9 +105,13 @@ exampleState =
_int = [32, 56], _int = [32, 56],
_float = [3.23, 9.235], _float = [3.23, 9.235],
_bool = [True, False], _bool = [True, False],
_string = ["abc", "123"],
_char = ['d', 'e', 'f'],
_parameter = [], _parameter = [],
_intVector = [[1, 2], [5, 6, 8]], _intVector = [[1, 2], [5, 6, 8]],
_floatVector = [[1.234, 9.21], [5.42, 6.221, 8.5493]], _floatVector = [[1.234, 9.21], [5.42, 6.221, 8.5493]],
_boolVector = [[True, False], [False, False, True]], _boolVector = [[True, False], [False, False, True]],
_stringVector = [["this is a sentence", "this is also a sentence"], ["s0", "s1", "s2"]],
_charVector = [['z', 'x'], ['r', 'a', 't', 'l']],
_input = Map.empty _input = Map.empty
} }