mirror of
https://github.com/irmen/prog8.git
synced 2025-11-02 13:16:07 +00:00
add if-expression versions for the conditionals if_cc, if_cs, if_vc etc
This commit is contained in:
@@ -66,6 +66,13 @@ class AstToSourceTextConverter(val output: (text: String) -> Unit, val program:
|
||||
ifExpr.falsevalue.accept(this)
|
||||
}
|
||||
|
||||
override fun visit(branchExpr: BranchConditionExpression) {
|
||||
output("if_${branchExpr.condition.name.lowercase()} ")
|
||||
branchExpr.truevalue.accept(this)
|
||||
output(" else ")
|
||||
branchExpr.falsevalue.accept(this)
|
||||
}
|
||||
|
||||
override fun visit(continueStmt: Continue) {
|
||||
output("continue")
|
||||
}
|
||||
|
||||
@@ -551,10 +551,10 @@ class Antlr2KotlinVisitor(val source: SourceCode): AbstractParseTreeVisitor<Node
|
||||
return IfExpression(condition, truevalue, falsevalue, ctx.toPosition())
|
||||
}
|
||||
|
||||
override fun visitBranchcondition_expression(ctx: Branchcondition_expressionContext): IfExpression {
|
||||
val branchcondition = branchCondition(ctx.branchcondition())
|
||||
override fun visitBranchcondition_expression(ctx: Branchcondition_expressionContext): BranchConditionExpression {
|
||||
val condition = branchCondition(ctx.branchcondition())
|
||||
val (truevalue, falsevalue) = ctx.expression().map { it.accept(this) as Expression }
|
||||
throw SyntaxError("branchcondition expression not yet supported", ctx.toPosition())
|
||||
return BranchConditionExpression(condition, truevalue, falsevalue, ctx.toPosition())
|
||||
}
|
||||
|
||||
override fun visitBranch_stmt(ctx: Branch_stmtContext): ConditionalBranch {
|
||||
|
||||
@@ -1660,6 +1660,42 @@ class IfExpression(var condition: Expression, var truevalue: Expression, var fal
|
||||
}
|
||||
}
|
||||
|
||||
class BranchConditionExpression(var condition: BranchCondition, var truevalue: Expression, var falsevalue: Expression, override val position: Position) : Expression() {
|
||||
|
||||
override lateinit var parent: Node
|
||||
|
||||
override fun linkParents(parent: Node) {
|
||||
this.parent = parent
|
||||
truevalue.linkParents(this)
|
||||
falsevalue.linkParents(this)
|
||||
}
|
||||
|
||||
override val isSimple: Boolean = truevalue.isSimple && falsevalue.isSimple
|
||||
|
||||
override fun constValue(program: Program) = null
|
||||
override fun toString() = "BranchExpr(cond=$condition, true=$truevalue, false=$falsevalue, pos=$position)"
|
||||
override fun accept(visitor: IAstVisitor) = visitor.visit(this)
|
||||
override fun accept(visitor: AstWalker, parent: Node) = visitor.visit(this, parent)
|
||||
override fun referencesIdentifier(nameInSource: List<String>): Boolean = truevalue.referencesIdentifier(nameInSource) || falsevalue.referencesIdentifier(nameInSource)
|
||||
override fun inferType(program: Program): InferredTypes.InferredType {
|
||||
val t1 = truevalue.inferType(program)
|
||||
val t2 = falsevalue.inferType(program)
|
||||
if(t1==t2) return t1
|
||||
if(t1.isPointer && t2.isUnsignedWord || t1.isUnsignedWord && t2.isPointer) return InferredTypes.knownFor(BaseDataType.UWORD)
|
||||
return InferredTypes.unknown()
|
||||
}
|
||||
|
||||
override fun copy(): Expression = BranchConditionExpression(condition, truevalue.copy(), falsevalue.copy(), position)
|
||||
|
||||
override fun replaceChildNode(node: Node, replacement: Node) {
|
||||
if(replacement !is Expression)
|
||||
throw FatalAstException("invalid replace")
|
||||
else if(node===truevalue) truevalue=replacement
|
||||
else if(node===falsevalue) falsevalue=replacement
|
||||
else throw FatalAstException("invalid replace")
|
||||
}
|
||||
}
|
||||
|
||||
class PtrDereference(
|
||||
val chain: List<String>,
|
||||
val derefLast: Boolean,
|
||||
|
||||
@@ -114,6 +114,7 @@ abstract class AstWalker {
|
||||
open fun before(expr: BinaryExpression, parent: Node): Iterable<IAstModification> = noModifications
|
||||
open fun before(expr: PrefixExpression, parent: Node): Iterable<IAstModification> = noModifications
|
||||
open fun before(ifExpr: IfExpression, parent: Node): Iterable<IAstModification> = noModifications
|
||||
open fun before(branchExpr: BranchConditionExpression, parent: Node): Iterable<IAstModification> = noModifications
|
||||
open fun before(forLoop: ForLoop, parent: Node): Iterable<IAstModification> = noModifications
|
||||
open fun before(repeatLoop: RepeatLoop, parent: Node): Iterable<IAstModification> = noModifications
|
||||
open fun before(unrollLoop: UnrollLoop, parent: Node): Iterable<IAstModification> = noModifications
|
||||
@@ -165,6 +166,7 @@ abstract class AstWalker {
|
||||
open fun after(expr: BinaryExpression, parent: Node): Iterable<IAstModification> = noModifications
|
||||
open fun after(expr: PrefixExpression, parent: Node): Iterable<IAstModification> = noModifications
|
||||
open fun after(ifExpr: IfExpression, parent: Node): Iterable<IAstModification> = noModifications
|
||||
open fun after(branchExpr: BranchConditionExpression, parent: Node): Iterable<IAstModification> = noModifications
|
||||
open fun after(forLoop: ForLoop, parent: Node): Iterable<IAstModification> = noModifications
|
||||
open fun after(repeatLoop: RepeatLoop, parent: Node): Iterable<IAstModification> = noModifications
|
||||
open fun after(unrollLoop: UnrollLoop, parent: Node): Iterable<IAstModification> = noModifications
|
||||
@@ -510,6 +512,13 @@ abstract class AstWalker {
|
||||
track(after(ifExpr, parent), ifExpr, parent)
|
||||
}
|
||||
|
||||
fun visit(branchExpr: BranchConditionExpression, parent: Node) {
|
||||
track(before(branchExpr, parent), branchExpr, parent)
|
||||
branchExpr.truevalue.accept(this, branchExpr)
|
||||
branchExpr.falsevalue.accept(this, branchExpr)
|
||||
track(after(branchExpr, parent), branchExpr, parent)
|
||||
}
|
||||
|
||||
fun visit(inlineAssembly: InlineAssembly, parent: Node) {
|
||||
track(before(inlineAssembly, parent), inlineAssembly, parent)
|
||||
track(after(inlineAssembly, parent), inlineAssembly, parent)
|
||||
|
||||
@@ -100,6 +100,11 @@ interface IAstVisitor {
|
||||
ifExpr.falsevalue.accept(this)
|
||||
}
|
||||
|
||||
fun visit(branchExpr: BranchConditionExpression) {
|
||||
branchExpr.truevalue.accept(this)
|
||||
branchExpr.falsevalue.accept(this)
|
||||
}
|
||||
|
||||
fun visit(label: Label) {
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user