mathpan/main.go

346 lines
7.2 KiB
Go
Raw Normal View History

2022-11-20 22:51:09 -07:00
package main
import "bytes"
import "image"
import _ "embed"
import _ "image/png"
import "git.tebibyte.media/sashakoshka/stone"
import _ "git.tebibyte.media/sashakoshka/stone/backends/x"
//go:embed icon/icon64.png
var iconBytes []byte
var application = &stone.Application { }
var expressionRoot Expression
var inputBuffer stone.DamageBuffer
var showLeftColumn bool
2022-11-21 23:02:42 -07:00
var showEndResult bool = true
var showSigned bool = true
var showFloat bool
2022-11-20 22:51:09 -07:00
func main () {
application.SetTitle("MathPan")
application.SetSize(64, 12)
2022-11-20 22:51:09 -07:00
icon, _, err := image.Decode(bytes.NewReader(iconBytes))
if err != nil { panic(err) }
application.SetIcon([]image.Image { icon })
application.OnStart(onStart)
application.OnResize(redraw)
application.OnPress(onPress)
err = application.Run()
if err != nil { panic(err) }
}
2022-11-21 22:28:26 -07:00
func onPress (button stone.Button, modifiers stone.Modifiers) {
2022-11-20 22:51:09 -07:00
switch button {
case ',':
2022-11-20 22:51:09 -07:00
if selectedExpression == nil { break }
newSelection := selectedExpression.Parent()
if newSelection != nil {
selectedExpression = newSelection
}
redraw()
application.Draw()
case stone.KeyLeft:
if selectedExpression == nil { break }
newSelection := selectedExpression.Previous()
if newSelection != nil {
selectedExpression = newSelection
}
redraw()
application.Draw()
case stone.KeyRight:
if selectedExpression == nil { break }
newSelection := selectedExpression.Next()
if newSelection != nil {
selectedExpression = newSelection
}
redraw()
application.Draw()
case '[':
insertGeneric (&Operation {
floating: showFloat,
}, modifiers.Alt, true, false)
redraw()
application.Draw()
case ']':
insertGeneric (&Operation {
floating: showFloat,
}, modifiers.Alt, false, false)
2022-11-20 22:51:09 -07:00
redraw()
application.Draw()
2022-11-23 19:33:37 -07:00
case '\\':
insertOperation('?', false, true)
redraw()
application.Draw()
2022-11-20 22:51:09 -07:00
2022-11-20 23:07:46 -07:00
case
'+', '-', '*', '/', 'p', 'P', '%', '|', '~', '&', '^', 'm',
'l', 'r':
2022-11-20 23:07:46 -07:00
2022-11-23 19:33:37 -07:00
insertOperation(rune(button), modifiers.Alt, false)
2022-11-20 22:51:09 -07:00
redraw()
application.Draw()
case stone.KeyDelete:
if selectedExpression == nil { break }
parent := selectedExpression.Parent()
if parent == nil {
expressionRoot = nil
selectedExpression = nil
} else {
previous := selectedExpression.Parent().Previous()
next := selectedExpression.Parent().Next()
selectedExpression.Parent().Disown(selectedExpression)
if next == nil {
selectedExpression = previous
} else {
selectedExpression = next
}
}
redraw()
application.Draw()
case stone.KeyBackspace:
if selectedExpression == nil { break }
parent := selectedExpression.Parent()
switch selectedExpression.(type) {
case *IntegerLiteral:
integer := selectedExpression.(*IntegerLiteral)
if integer.value == 0 { break }
integer.value /= int64(integer.displayRadix)
goto stopBackspacing
}
if parent == nil {
expressionRoot = nil
selectedExpression = nil
} else {
previous := selectedExpression.Parent().Previous()
selectedExpression.Parent().Disown(selectedExpression)
if previous != nil {
selectedExpression = previous
}
}
stopBackspacing:
redraw()
application.Draw()
case ' ':
insertNumber(0, modifiers.Alt, modifiers.Shift)
2022-11-20 22:51:09 -07:00
redraw()
application.Draw()
case
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f':
value := rune(button)
if value >= 'a' {
value -= 'a'
value += 10
} else {
value -= '0'
}
switch selectedExpression.(type) {
case *Operation, nil:
insertNumber(int64(value), modifiers.Alt, false)
2022-11-20 22:51:09 -07:00
case *IntegerLiteral:
integer := selectedExpression.(*IntegerLiteral)
integer.value *= int64(integer.displayRadix)
2022-11-21 23:19:35 -07:00
if integer.value < 0 {
integer.value -= int64(value)
} else {
integer.value += int64(value)
}
2022-11-20 22:51:09 -07:00
}
redraw()
application.Draw()
case 'x':
switch selectedExpression.(type) {
case *IntegerLiteral:
integer := selectedExpression.(*IntegerLiteral)
switch integer.displayRadix {
case 2:
integer.displayRadix = 8
case 8:
integer.displayRadix = 10
case 10:
integer.displayRadix = 16
case 16:
integer.displayRadix = 2
}
}
redraw()
application.Draw()
case 'X':
switch selectedExpression.(type) {
case *IntegerLiteral:
integer := selectedExpression.(*IntegerLiteral)
switch integer.displayRadix {
case 2:
integer.displayRadix = 16
case 8:
integer.displayRadix = 2
case 10:
integer.displayRadix = 8
case 16:
integer.displayRadix = 10
}
}
redraw()
application.Draw()
2022-11-21 23:02:42 -07:00
case 's':
switch selectedExpression.(type) {
case *IntegerLiteral:
integer := selectedExpression.(*IntegerLiteral)
integer.value *= -1
}
redraw()
application.Draw()
case 'S':
2022-11-21 23:02:42 -07:00
showSigned = !showSigned
redraw()
application.Draw()
case 'q':
2022-11-21 23:02:42 -07:00
showEndResult = !showEndResult
redraw()
application.Draw()
2022-11-23 09:14:42 -07:00
case 'z':
switch selectedExpression.(type) {
case *Operation, nil:
operation := selectedExpression.(*Operation)
operation.floating = !operation.floating
}
redraw()
application.Draw()
case 'Z':
2022-11-23 09:14:42 -07:00
showFloat = !showFloat
redraw()
application.Draw()
2022-11-20 22:51:09 -07:00
}
}
2022-11-23 19:33:37 -07:00
func insertOperation (symbol rune, swap, encompass bool) {
2022-11-20 22:51:09 -07:00
var opcode Opcode
switch (symbol) {
2022-11-23 19:33:37 -07:00
case '?': opcode = OpcodeUnknown
2022-11-20 22:51:09 -07:00
case '+': opcode = OpcodeAdd
case '-': opcode = OpcodeSubtract
case '*': opcode = OpcodeMultiply
case '/': opcode = OpcodeDivide
case 'p': opcode = OpcodePower
case 'P': opcode = OpcodeRoot
2022-11-20 22:51:09 -07:00
case '%': opcode = OpcodeModulo
case '|': opcode = OpcodeOr
case '~': opcode = OpcodeNot
case '&': opcode = OpcodeAnd
case '^': opcode = OpcodeXor
case 'l': opcode = OpcodeLeftShift
case 'r': opcode = OpcodeRightShift
2022-11-20 22:51:09 -07:00
case 'm': opcode = OpcodeMean
}
2022-11-23 19:33:37 -07:00
newExpression := Operation { opcode: opcode, floating: showFloat }
if encompass {
if selectedExpression == nil { return }
parent := selectedExpression.Parent()
newExpression.Adopt(selectedExpression)
if parent == nil {
expressionRoot = &newExpression
} else {
parent.Swap(selectedExpression, &newExpression)
}
selectedExpression = &newExpression
return
}
2022-11-20 22:51:09 -07:00
operation, isOperation := selectedExpression.(*Operation)
if isOperation {
2022-11-21 22:28:26 -07:00
if operation.opcode == OpcodeUnknown || swap {
2022-11-20 22:51:09 -07:00
operation.opcode = opcode
return
}
}
2022-11-23 19:33:37 -07:00
insertGeneric(&newExpression, swap, false, true)
2022-11-20 22:51:09 -07:00
}
func insertNumber (value int64, swap, before bool) {
insertGeneric (&IntegerLiteral {
displayRadix: 10,
value: value,
}, swap, before, true)
}
2022-11-20 22:51:09 -07:00
func insertGeneric (expression Expression, swap, before, fillIn bool) {
// if there are no expressions, place this at root
if selectedExpression == nil || expressionRoot == nil {
2022-11-20 22:51:09 -07:00
expressionRoot = expression
selectedExpression = expression
2022-11-20 22:51:09 -07:00
return
}
// prefer filling an empty operation
2022-11-20 22:51:09 -07:00
operation, isOperation := selectedExpression.(*Operation)
if fillIn && isOperation && len(operation.operands) == 0 {
2022-11-20 22:51:09 -07:00
operation.Adopt(expression)
selectedExpression = expression
2022-11-20 22:51:09 -07:00
return
}
// insert expression into the parent operation
parent := selectedExpression.Parent()
if parent == nil { return }
if swap {
parent.Swap(selectedExpression, expression)
2022-11-20 22:51:09 -07:00
} else {
parent.Insert(selectedExpression, expression, !before)
2022-11-20 22:51:09 -07:00
}
selectedExpression = expression
2022-11-20 22:51:09 -07:00
}
func onStart () {
redraw()
}