Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Function inside Function #34

Merged
merged 3 commits into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions Main.pseudo
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
Gcd(a,b)
// still to fix
if(a=0)
then return b
r <- b mod a
return Gcd(r, a)

rozNWD(j,k)
//foo
if (j<0 or j>k)
if (j<0 or j>=k)
then return None
//bar
if (j = 0)
then return (k, 0, 1) //info dla G
then return (k, 0, 1)
r <- k mod j
(d, xp, yp) <- rozNWD(r, j)
x <- yp - k /f j * xp
Expand Down
12 changes: 12 additions & 0 deletions src/main/scala/org/PseudoLang/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ object Main extends App {
val name = "Generated"
FileManager.saveCodeToFile("", name, transpiled, "py")

def runParserWD(): Unit = {
println("========================= INPUT ============================")
println(code)
println("========================== AST =============================")
println(Debug.pretty(parsed.toString))
println("========================== CODE ============================")
println(parsed.show())
println("============================================================")
}

def runWithDebugging(): Unit = {
println("======================= PSEUDO LANG ========================")
println("========================= INPUT ============================")
Expand All @@ -41,4 +51,6 @@ object Main extends App {
}

runWithoutDebugging()
// runWithDebugging()
// runParserWD()
}
25 changes: 14 additions & 11 deletions src/main/scala/org/PseudoLang/Transpiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import scala.sys.process.stdout
* through AST
*/
object Transpiler {
def run(ast: AST): String = transpile(ast).build()
def run(ast: AST): String = transpile(ast).build()
def transpile(ast: AST): Repr.Builder = traverse(0, ast.elems)

/**
Expand Down Expand Up @@ -72,10 +72,7 @@ object Transpiler {
a: AST.Array,
rest: List[AST.Elem]
): Repr.Builder = {
R + a.name + traverseParens(a.elems) + traverse(
indent,
rest
)
R + a.name + traverseParens(a.elems) + traverse(indent, rest)
}

private def transpileRepeatUntil(
Expand All @@ -87,7 +84,7 @@ object Transpiler {
case b: AST.Block => R + traverseBlock(b)
case oth => R + oth
}
R + "while True:" + bRepr + AST
R + AST.Newline() + indent + "while True:" + bRepr + AST
.Newline() + l.block
.asInstanceOf[AST.Block]
.indent + "if not " + traverseParens(l.condition) + AST
Expand All @@ -103,7 +100,8 @@ object Transpiler {
rest: List[AST.Elem]
): Repr.Builder = {
val bRepr = matchBlock(l.block)
R + "for " + traverseParens(l.condition) + ":" + bRepr + traverse(
R + AST
.Newline() + indent + "for " + traverseParens(l.condition) + ":" + bRepr + traverse(
indent,
rest
)
Expand All @@ -115,7 +113,7 @@ object Transpiler {
rest: List[AST.Elem]
): Repr.Builder = {
val bRepr = matchBlock(l.block)
R + "while True:" + bRepr + AST
R + AST.Newline() + indent + "while True:" + bRepr + AST
.Newline() + l.block
.asInstanceOf[AST.Block]
.indent + "if " + traverseParens(l.condition) + AST
Expand All @@ -131,7 +129,8 @@ object Transpiler {
rest: List[AST.Elem]
): Repr.Builder = {
val bRepr = matchBlock(l.block)
R + "while " + traverseParens(l.condition) + ":" + bRepr + traverse(
R + AST
.Newline() + indent + "while " + traverseParens(l.condition) + ":" + bRepr + traverse(
indent,
rest
)
Expand Down Expand Up @@ -175,7 +174,8 @@ object Transpiler {
): Repr.Builder = {
val ifRepr = R + "if" + traverseParens(i.condition) + ":"
val bRepr = matchBlock(i.block)
R + ifRepr + bRepr + traverse(indent, rest)
R + AST.Newline() + indent + ifRepr + bRepr + traverse(indent, rest) + AST
.Newline() + indent
}

private def transpileFunction(
Expand All @@ -186,7 +186,10 @@ object Transpiler {
f.block match {
case b: AST.Block =>
val fDecl = R + "def " + f.name + traverseParens(f.args) + ":"
R + fDecl + traverseBlock(b) + traverse(indent, rest)
R + AST.Newline() + indent + fDecl + traverseBlock(b) + traverse(
indent,
rest
)
case _ =>
if (f.name.name == "length") {
R + "len" + f.args + traverse(indent, rest)
Expand Down
60 changes: 34 additions & 26 deletions syntax/src/main/scala/org/PseudoLang/syntax/text/ast/AST.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ final case class AST(elems: List[AST.Elem]) extends Symbol {
}

object AST {
def apply(): AST = new AST(Nil)
def apply(elem: AST.Elem): AST = new AST(elem :: Nil)
def apply(): AST = new AST(Nil)
def apply(elem: AST.Elem): AST = new AST(elem :: Nil)
def apply(elems: AST.Elem*): AST = new AST(elems.toList)

/**
Expand Down Expand Up @@ -88,8 +88,8 @@ object AST {
val repr: Repr.Builder = R + Le + " " + marker + " " + Re
}
object Opr {
def apply(m: Opr.Marker) = new Opr(m, Empty(), Empty())
def apply(m: Opr.Marker, e: Elem) = new Opr(m, e, Empty())
def apply(m: Opr.Marker) = new Opr(m, Empty(), Empty())
def apply(m: Opr.Marker, e: Elem) = new Opr(m, e, Empty())
def apply(m: Opr.Marker, Le: Elem, Re: Elem): Opr = new Opr(m, Le, Re)

/**
Expand Down Expand Up @@ -134,7 +134,7 @@ object AST {
val repr: Repr.Builder = R + len
}
object Spacing {
def apply(): Spacing = new Spacing(1)
def apply(): Spacing = new Spacing(1)
def apply(len: Int): Spacing = new Spacing(len)
}

Expand All @@ -151,7 +151,7 @@ object AST {
}

object Comment {
val marker: String = "//"
val marker: String = "//"
def apply(str: String): Comment = new Comment(str)
}

Expand All @@ -169,7 +169,7 @@ object AST {
}

object Array {
def apply(par: AST.Parens): Array = new Array(AST.Empty(), par)
def apply(par: AST.Parens): Array = new Array(AST.Empty(), par)
def apply(elem: AST.Elem, par: AST.Parens): Array = new Array(elem, par)
}

Expand All @@ -189,8 +189,8 @@ object AST {
}

object Parens {
def apply(): Parens = new Parens('(', ')', List())
def apply(elem: AST.Elem): Parens = new Parens('(', ')', elem :: Nil)
def apply(): Parens = new Parens('(', ')', List())
def apply(elem: AST.Elem): Parens = new Parens('(', ')', elem :: Nil)
def apply(elems: AST.Elem*): Parens = new Parens('(', ')', elems.toList)
}

Expand All @@ -204,18 +204,18 @@ object AST {
* @param block - method's definition
* @param args - method's arguments
*/
case class Func(name: Var, block: AST.Elem, args: AST.Parens) extends Elem {
case class Func(name: Var, args: Parens, block: Elem) extends Elem {
val repr: Repr.Builder = R + name + args + block
}
object Func {
def apply(name: Var): Func =
new Func(name, AST.Empty(), AST.Parens())
new Func(name, AST.Parens(), AST.Empty())
def apply(name: Var, block: AST.Block): Func =
new Func(name, block, AST.Parens())
new Func(name, AST.Parens(), block)
def apply(name: Var, par: AST.Parens): Func =
new Func(name, AST.Empty(), par)
new Func(name, par, AST.Empty())
def apply(name: Var, block: AST.Block, par: AST.Parens): Func =
new Func(name, block, par)
new Func(name, par, block)

/**
* This is the [[AST.Func.Return]].
Expand All @@ -226,8 +226,8 @@ object AST {
val repr: Repr.Builder = R + "Return " + value
}
case object Return {
def apply(): Return = new Return(Nil)
def apply(value: AST.Elem): Return = new Return(value :: Nil)
def apply(): Return = new Return(Nil)
def apply(value: AST.Elem): Return = new Return(value :: Nil)
def apply(value: AST.Elem*): Return = new Return(value.toList)
}
}
Expand Down Expand Up @@ -258,8 +258,8 @@ object AST {
val repr: Repr.Builder = R + "Then " + e
}
object ThenCase {
def apply(): ThenCase = new ThenCase(Nil)
def apply(e: AST.Elem): ThenCase = new ThenCase(e :: Nil)
def apply(): ThenCase = new ThenCase(Nil)
def apply(e: AST.Elem): ThenCase = new ThenCase(e :: Nil)
def apply(e: AST.Elem*): ThenCase = new ThenCase(e.toList)
}

Expand All @@ -272,8 +272,8 @@ object AST {
val repr: Repr.Builder = R + "Else " + e
}
object ElseCase {
def apply(): ElseCase = new ElseCase(Nil)
def apply(e: AST.Elem): ElseCase = new ElseCase(e :: Nil)
def apply(): ElseCase = new ElseCase(Nil)
def apply(e: AST.Elem): ElseCase = new ElseCase(e :: Nil)
def apply(e: AST.Elem*): ElseCase = new ElseCase(e.toList)
}
}
Expand Down Expand Up @@ -358,16 +358,24 @@ object AST {
*/
case class Block(indent: Int, elems: List[Elem]) extends Elem {
val repr: Repr.Builder = R + Newline() + indent + elems.map {
case elem: Newline => R + elem + indent
case b: AST.Block => R + b + indent
case elem => R + elem
case elem: Newline => R + elem + indent
case b: AST.Block => R + b + indent
case b: AST.If => R + b + indent
case b: AST.While => R + b + indent
case b: AST.For => R + b + indent
case b: AST.DoWhile => R + b + indent
case b: AST.RepeatUntil => R + b + indent
case b: AST.If.ThenCase => R + b + indent
case b: AST.If.ElseCase => R + b + indent
case b: AST.Func.Return => R + b + indent
case elem => R + elem
} + Newline()
}
object Block {
def apply(): Block = new Block(0, Nil)
def apply(elem: AST.Elem): Block = new Block(0, elem :: Nil)
def apply(): Block = new Block(0, Nil)
def apply(elem: AST.Elem): Block = new Block(0, elem :: Nil)
def apply(elems: AST.Elem*): Block = new Block(0, elems.toList)
def apply(indent: Int): Block = new Block(indent, Nil)
def apply(indent: Int): Block = new Block(indent, Nil)
def apply(indent: Int, elem: AST.Elem): Block =
new Block(indent, elem :: Nil)
def apply(indent: Int, elems: AST.Elem*): Block =
Expand Down
Loading