Skip to content

Commit

Permalink
Implemented the exponentiation operator, added a status bar, and made…
Browse files Browse the repository at this point in the history
… code in the applet execute in a separate thread (i. e. concurrently)
  • Loading branch information
bluebear94 committed Jan 15, 2014
1 parent a159e45 commit 4e80495
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 92 deletions.
5 changes: 3 additions & 2 deletions src/main/scala/cmdreader/Global.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ object Global {
}
var root: File = new File("amw/")
var current: File = root
var currentAlias = "root"
val TWO = new BigInteger("2")
val vM = 0
val vm = 4
val vr = 2
val vm = 5
val vr = 0
val version = vM + "." + vm + "." + vr
}
1 change: 1 addition & 0 deletions src/main/scala/cmdreader/std/Loader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ class Loader {
Global.liblist("std").loadCmd("ECons")
Global.liblist("std").loadCmd("UONegate")
Global.liblist("std").loadCmd("UONot")
Global.liblist("std").loadCmd("OTt")
}
}
21 changes: 21 additions & 0 deletions src/main/scala/cmdreader/std/OTt.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmdreader.std

import cmdreader._
import types._
import util._
import java.math.BigInteger

class OTt extends CommandOperator {
override def getName(): String = "tt"
override def getOpAlias() = "^"
override def isValidArg0(n: Int) = n == 2
override def apply(args: Array[Type]): Type = {
args.tail.fold(
args.head
)(MathUtil.tt(_, _))
}
def getPrecedence() = PStandard.EXPONENT
def isReversed() = false
def hasAssignmentEquiv() = true
def getDoubleBase() = Some(new TMountain(new BigInteger("2")))
}
136 changes: 93 additions & 43 deletions src/main/scala/gui/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import cmdreader.Global
import parse.ast._
import scala.util.parsing.input.CharSequenceReader
import types.TVoid
import scala.collection.mutable.ArrayBuffer

// I'm a complete noob to this...

Expand All @@ -22,27 +23,96 @@ object Main extends SimpleSwingApplication {
val BUSY = 1
val ASKING = 2
var status = IDLE
def setSt(s: Int) = {
status = s
statusBar3.usb
statusBar3.repaint
}
def updDirs = {
statusBar1.usb
statusBar1.repaint
}
val homeScn = new TextArea(20, 80) { // the middle panel, holding previous operations
font = mono
editable = false
charWrap = true
text = "Welcome to Amethyst " + Global.version + ".\n\n" +
(if (mono.getFamily != "DejaVu Sans Mono") "Please install the DejaVu fonts.\n" else "")
tooltip = "The homescreen. Holds previous operations and console output."
}
val drawScn = new BufferedCanvas { // the top panel, for drawing
preferredSize = new Dimension(640, 480)
tooltip = "A canvas for graphical operations."
}
val statusBar1 = new Label {
text = {
"CUR: " + Global.current + " (" + Global.currentAlias + ")"
}
font = mono
horizontalAlignment = Alignment.Left
def usb = {
text = "CUR: " + Global.current + " (" + Global.currentAlias + ")"
}
}
val statusBar2 = new Label {
text = {
"ROOT: " + Global.root
}
font = mono
horizontalAlignment = Alignment.Left
}
val statusBar3 = new Label {
text = {
Array[String]("IDLE", "BUSY", "ASKING").apply(status)
}
font = mono
horizontalAlignment = Alignment.Left
def usb = {
text = Array[String]("IDLE", "BUSY", "ASKING").apply(status)
}
}
def insertAtCaret(s: String) = {
val curPos = inputArea.caret.dot
inputArea.text = inputArea.text.substring(0, curPos) + s + inputArea.text.substring(curPos)
}
def runCodeConcurrently() = {
val newThread = new ExecutionThread
newThread.start
}
def runCode() = {
val toRun = inputArea.text
if (toRun != "") {
println(toRun)
inputArea.text = ""
setSt(BUSY)
try {
val bc = WholeParser.parse(toRun, p)
val tp = Global.top
tp.bytecode = bc
tp.run
println(tp.ans + "\n")
} catch {
case e: RuntimeException => {
println(e.getMessage)
System.out.println(e.getMessage)
e.printStackTrace
}
}
setSt(IDLE)
}
}
val inputArea = new TextArea(5, 80) { // the bottom panel which holds the user's input
font = mono
tooltip = "Enter your code here!"
charWrap = true
}
def top = new MainFrame {
Global.loadLib("std")
p.loadOps
title = "Amethyst " + Global.version
val inputArea = new TextArea(5, 80) { // the bottom panel which holds the user's input
font = mono
tooltip = "Enter your code here!"
charWrap = true
}

val inputScroll = new ScrollPane(inputArea)
val homeScroll = new ScrollPane(homeScn)
val drawScn = new BufferedCanvas { // the top panel, for drawing
preferredSize = new Dimension(640, 480)
}
val runButton = new Button {
text = "Run (Ctrl + Enter)"
}
Expand All @@ -56,57 +126,31 @@ object Main extends SimpleSwingApplication {
text = ""
}
val buttons = new FlowPanel(lambdaButton, harpoonButton, superMinusButton, runButton)
val inputAndButtons = new BorderPanel {
layout(inputScroll) = Center
layout(buttons) = South
val statusBar = new BoxPanel(Orientation.Vertical) {
contents += statusBar1
contents += statusBar2
contents += statusBar3
}
contents = new BorderPanel {
layout(drawScn) = North
layout(homeScroll) = Center
layout(inputAndButtons) = South
contents = new BoxPanel(Orientation.Vertical) {
contents ++= ArrayBuffer(drawScn, homeScroll, inputScroll, buttons, statusBar)
focusable = true
requestFocus
listenTo(keys, drawScn.keys, homeScroll.keys, inputArea.keys, buttons.keys, lambdaButton, harpoonButton, runButton, superMinusButton)
reactions += {
case KeyPressed(_, Key.Enter, m, _) => {
if ((m & 0xC0) != 0)
runCode()
runCodeConcurrently()
}
case ButtonClicked(component) => {
if (component == lambdaButton) insertAtCaret("λ")
if (component == harpoonButton) insertAtCaret("")
if (component == superMinusButton) insertAtCaret("")
if (component == runButton) runCode()
if (component == runButton) runCodeConcurrently()
}
}
}
size = new Dimension(640, 768)
def insertAtCaret(s: String) = {
val curPos = inputArea.caret.dot
inputArea.text = inputArea.text.substring(0, curPos) + s + inputArea.text.substring(curPos)
}
def runCode() = {
val toRun = inputArea.text
if (toRun != "") {
println(toRun)
inputArea.text = ""
status = BUSY
try {
val bc = WholeParser.parse(toRun, p)
val tp = Global.top
tp.bytecode = bc
tp.run
println(tp.ans + "\n")
} catch {
case e: RuntimeException => {
println(e.getMessage)
System.out.println(e.getMessage)
e.printStackTrace
}
}
status = IDLE
}
}

}
}

Expand All @@ -121,4 +165,10 @@ class BufferedCanvas extends Panel { // sigh, I have to make one myself
grf(bg)
repaint
}
}

// argh, finally have to implement multithreading

class ExecutionThread extends Thread {
override def run = Main.runCode
}
Loading

0 comments on commit 4e80495

Please sign in to comment.