Skip to content

Commit

Permalink
Added two more functions and fixed choking on escaped quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
bluebear94 committed Feb 8, 2014
1 parent b180cdb commit 1e3cc7c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/cmdreader/Global.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ object Global {
val TWO = new BigInteger("2")
val vM = 0
val vm = 5
val vr = 17
val vr = 18
val vrr = "-alpha"
val version = "v" + vM + "." + vm + "." + vr + vrr
val r: Random = new Random
Expand Down
2 changes: 2 additions & 0 deletions src/main/scala/cmdreader/std/Loader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,7 @@ class Loader {
Global.liblist("std").loadCmd("Len")
Global.liblist("std").loadCmd("QSort")
Global.liblist("std").loadCmd("MSort")
Global.liblist("std").loadCmd("Ribzpt")
Global.liblist("std").loadCmd("OAug")
}
}
21 changes: 21 additions & 0 deletions src/main/scala/cmdreader/std/OAug.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 OAug extends CommandOperator {
override def getName(): String = "aug"
override def getOpAlias() = "&+"
override def isValidArg0(n: Int) = n >= 2
override def apply(args: Array[Type]): Type = {
val trueLists = args.map(CollectionOps.decodeToList(_))
val newList = trueLists.fold(Nil)(_ ++ _)
CollectionOps.encodeFromList(newList, args(0).getType)
}
def getPrecedence() = PStandard.ADD_SUBT
def isReversed() = false
def hasAssignmentEquiv() = true
def getDoubleBase() = None
}
17 changes: 17 additions & 0 deletions src/main/scala/cmdreader/std/Ribzpt.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cmdreader.std

import cmdreader._
import types._
import scala.math.BigInt

class Ribzpt extends Command {
override def getName(): String = "ribzpt"
override def isValidArg0(n: Int): Boolean = n == 1
override def apply(args: Array[Type]): Type = {
val nb = args(0) match {
case n: TNumerical => n.intValue
case _ => return new TError(1)
}
TMountain(BigInt(nb, Global.r))
}
}
10 changes: 5 additions & 5 deletions src/main/scala/parse/ast/Expression.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package parse.ast
import types._
import run.RunningInstance
import scala.util.parsing.combinator._
import java.math.BigInteger
import scala.math.BigInt
import types._
import org.scalatest._
import scala.util.parsing.input.CharSequenceReader
Expand Down Expand Up @@ -383,8 +383,8 @@ class XprInt extends JavaTokenParsers with PackratParsers {
} | "\\$".r | "".r) ~ id)
.filter(w => !Keywords.keywords.contains(w._2)) ^^
{ s => Variable(s._1 + s._2) }
lazy val mountain: PackratParser[SBExpression] = wholeNumber ^^ { s => new Literal(new TMountain(new BigInteger(s))) }
lazy val hill: PackratParser[SBExpression] = """↼[-]?\d+""".r ^^ { s => new Literal(new THill(s.substring(1).toLong)) }
lazy val mountain: PackratParser[SBExpression] = wholeNumber ^^ { s => new Literal(new TMountain(BigInt(s))) }
lazy val hill: PackratParser[SBExpression] = """↼[-]?\d+""".r ^^ { s => new Literal(new THill(BigInt(s.substring(1)).toLong)) }
lazy val string: PackratParser[SBExpression] = stringLiteral ^^ { s =>
UnescapeString.unescape(s.substring(1, s.length - 1)) match {
case Some(ues) => Literal(new TString(ues))
Expand Down Expand Up @@ -545,9 +545,9 @@ class XprInt extends JavaTokenParsers with PackratParsers {
}
// now add the logical and and or parsers (short-circuit)
val andParser: PackratParser[(Expression, Expression) => Expression] = "&&" ^^^
{ (a: Expression, b: Expression) => Ternary(a, b, Literal(TMountain(BigInteger.ZERO))) }
{ (a: Expression, b: Expression) => Ternary(a, b, Literal(TMountain(0))) }
val orParser: PackratParser[(Expression, Expression) => Expression] = "||" ^^^
{ (a: Expression, b: Expression) => Ternary(a, Literal(TMountain(BigInteger.ONE)), b) }
{ (a: Expression, b: Expression) => Ternary(a, Literal(TMountain(1)), b) }
loadWithPrec(PStandard.CONJUNCTION, (andParser, false))
loadWithPrec(PStandard.DISJUNCTION, (orParser, false))
}
Expand Down
20 changes: 14 additions & 6 deletions src/main/scala/parse/ast/Preprocessor.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package parse.ast

/**
* Utilities for applying changes to Amethyst code for it to be parsed.
* @author bluebear94
*/
object Preprocessor {
def preprocessLn(line: String) = {
var quoteMode = false
var bs = false
var st = ""
def pop(c: Char) = {
val l = st.length - 1
Expand All @@ -14,19 +19,22 @@ object Preprocessor {
while (i < line.length && cont) {
val c = line.charAt(i)
c match {
case '\"' => quoteMode = !quoteMode
case '(' | '[' | '{' | '«' => st += c
case ')' => pop('(')
case ']' => pop('[')
case '}' => pop('{')
case '»' => pop('«')
case '\"' => if (!bs) {
quoteMode = !quoteMode
}
case '(' | '[' | '{' | '«' => if (!quoteMode) st += c
case ')' => if (!quoteMode) pop('(')
case ']' => if (!quoteMode) pop('[')
case '}' => if (!quoteMode) pop('{')
case '»' => if (!quoteMode) pop('«')
case '_' => if (!quoteMode) {
i -= 1
cont = false
}
case _ => ()
}
i += 1
bs = c == '\\'
}
var sub = line.substring(0, i) + (if (quoteMode) "\"" else "")
for (c <- st.reverse) {
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/parse/ast/WholeParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ object WholeParser {
def parse(code: String, p: XprInt) = {
def isNL(c: Char) = c == ';' || c == '\n'
val preprocessedCode = Preprocessor.preprocess(code)
println(preprocessedCode)
import p._
var bytes = Array[Bin]()
var rest = new p.PackratReader(new CharSequenceReader(preprocessedCode))
Expand Down

0 comments on commit 1e3cc7c

Please sign in to comment.