diff --git a/go.mod b/go.mod index fc449e9..1ef5de3 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,8 @@ module git.tebibyte.media/sashakoshka/mathpan -go 1.18 +go 1.19 -require git.tebibyte.media/sashakoshka/stone v0.0.0-20221122052135-ae514f5ae2db +require git.tebibyte.media/sashakoshka/stone v0.0.0-20221124013405-5a76bd0c223d require ( github.com/BurntSushi/freetype-go v0.0.0-20160129220410-b763ddbfe298 // indirect diff --git a/go.sum b/go.sum index 999d099..6e0eadd 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -git.tebibyte.media/sashakoshka/stone v0.0.0-20221122052135-ae514f5ae2db h1:2llsOVwxK5oK4E/RX21tzafQBc6N+H6MwRSQykCb6ss= -git.tebibyte.media/sashakoshka/stone v0.0.0-20221122052135-ae514f5ae2db/go.mod h1:ISnqmX6xvItOot3eW3YWLcNFeJrGpKetQGQniAjnU2A= +git.tebibyte.media/sashakoshka/stone v0.0.0-20221124013405-5a76bd0c223d h1:2uDDq+lHbhODq+6hmosmCcAaLOg55a7LOIhiJMzeSds= +git.tebibyte.media/sashakoshka/stone v0.0.0-20221124013405-5a76bd0c223d/go.mod h1:ISnqmX6xvItOot3eW3YWLcNFeJrGpKetQGQniAjnU2A= github.com/BurntSushi/freetype-go v0.0.0-20160129220410-b763ddbfe298 h1:1qlsVAQJXZHsaM8b6OLVo6muQUQd4CwkH/D3fnnbHXA= github.com/BurntSushi/freetype-go v0.0.0-20160129220410-b763ddbfe298/go.mod h1:D+QujdIlUNfa0igpNMk6UIvlb6C252URs4yupRUV4lQ= github.com/BurntSushi/graphics-go v0.0.0-20160129215708-b43f31a4a966 h1:lTG4HQym5oPKjL7nGs+csTgiDna685ZXjxijkne828g= diff --git a/keyboard.xcf b/keyboard.xcf new file mode 100644 index 0000000..d228a42 Binary files /dev/null and b/keyboard.xcf differ diff --git a/main.go b/main.go index d7453ce..36128ad 100644 --- a/main.go +++ b/main.go @@ -37,7 +37,7 @@ func main () { func onPress (button stone.Button, modifiers stone.Modifiers) { switch button { - case stone.KeyUp: + case ',': if selectedExpression == nil { break } newSelection := selectedExpression.Parent() @@ -71,13 +71,22 @@ func onPress (button stone.Button, modifiers stone.Modifiers) { application.Draw() case '[': - insertGeneric(&Operation { floating: showFloat }, modifiers.Alt) + insertGeneric (&Operation { + floating: showFloat, + }, modifiers.Alt, true, false) + redraw() + application.Draw() + + case ']': + insertGeneric (&Operation { + floating: showFloat, + }, modifiers.Alt, false, false) redraw() application.Draw() case - '+', '-', '*', '/', 'p', 'r', '%', '|', '~', '&', '^', 'm', - '<', '>': + '+', '-', '*', '/', 'p', 'P', '%', '|', '~', '&', '^', 'm', + 'l', 'r': insertOperation(rune(button), modifiers.Alt) redraw() @@ -137,9 +146,7 @@ func onPress (button stone.Button, modifiers stone.Modifiers) { application.Draw() case ' ': - insertGeneric ( - &IntegerLiteral { displayRadix: 10 }, - modifiers.Alt) + insertNumber(0, modifiers.Alt, modifiers.Shift) redraw() application.Draw() @@ -157,10 +164,7 @@ func onPress (button stone.Button, modifiers stone.Modifiers) { switch selectedExpression.(type) { case *Operation, nil: - insertGeneric (&IntegerLiteral { - displayRadix: 10, - value: int64(value), - }, modifiers.Alt) + insertNumber(int64(value), modifiers.Alt, false) case *IntegerLiteral: integer := selectedExpression.(*IntegerLiteral) @@ -228,12 +232,12 @@ func onPress (button stone.Button, modifiers stone.Modifiers) { redraw() application.Draw() - case 'R': + case 'q': showEndResult = !showEndResult redraw() application.Draw() - case 'F': + case 'Z': showFloat = !showFloat redraw() application.Draw() @@ -248,14 +252,14 @@ func insertOperation (symbol rune, swap bool) { case '*': opcode = OpcodeMultiply case '/': opcode = OpcodeDivide case 'p': opcode = OpcodePower - case 'r': opcode = OpcodeRoot + case 'P': opcode = OpcodeRoot case '%': opcode = OpcodeModulo case '|': opcode = OpcodeOr case '~': opcode = OpcodeNot case '&': opcode = OpcodeAnd case '^': opcode = OpcodeXor - case '<': opcode = OpcodeLeftShift - case '>': opcode = OpcodeRightShift + case 'l': opcode = OpcodeLeftShift + case 'r': opcode = OpcodeRightShift case 'm': opcode = OpcodeMean } @@ -268,48 +272,41 @@ func insertOperation (symbol rune, swap bool) { } newExpression := &Operation { opcode: opcode, floating: showFloat } - insertGeneric(newExpression, swap) + insertGeneric(newExpression, swap, false, true) } -func insertGeneric (expression Expression, swap bool) { - defer func () { +func insertNumber (value int64, swap, before bool) { + insertGeneric (&IntegerLiteral { + displayRadix: 10, + value: value, + }, swap, before, true) +} + +func insertGeneric (expression Expression, swap, before, fillIn bool) { + // if there are no expressions, place this at root + if selectedExpression == nil || expressionRoot == nil { + expressionRoot = expression selectedExpression = expression - } () - - if expressionRoot == nil { - // if there are no expressions, place this one down in the empty - // void - expressionRoot = expression return } - - parent := selectedExpression.Parent() - if swap { - // if alt is held, swap the selected expression for the new one - if parent == nil { - expressionRoot = expression - } else { - selectedExpression.Parent().Swap(selectedExpression, expression) - } - return - } - + // prefer filling an empty operation operation, isOperation := selectedExpression.(*Operation) - - if isOperation && (len(operation.operands) < 1 || parent == nil) { - // if we have an empty operation selected, always insert the - // new expression into it + if fillIn && isOperation && len(operation.operands) == 0 { operation.Adopt(expression) + selectedExpression = expression return } - // else, insert the new expression after the selected expression - if parent == nil { - expressionRoot = expression + // insert expression into the parent operation + parent := selectedExpression.Parent() + if parent == nil { return } + if swap { + parent.Swap(selectedExpression, expression) } else { - selectedExpression.Parent().Insert(selectedExpression, expression) + parent.Insert(selectedExpression, expression, !before) } + selectedExpression = expression } func onStart () { diff --git a/tree.go b/tree.go index e643385..b4745f3 100644 --- a/tree.go +++ b/tree.go @@ -96,21 +96,22 @@ func (operation *Operation) adopt (child Expression) { } } -func (operation *Operation) Insert (after Expression, child Expression) { +func (operation *Operation) Insert (around, child Expression, after bool) { var operand Expression var index int var found bool for index, operand = range operation.operands { - if operand == after { found = true; break } + if operand == around { found = true; break } } if !found { return } - index ++ + if after { index ++ } operation.operands = append(operation.operands, nil) copy(operation.operands[index + 1:], operation.operands[index:]) operation.operands[index] = child operation.adopt(child) + } func (operation *Operation) Disown (child Expression) {