More expression types

This commit is contained in:
RochesterX
2026-02-02 14:03:31 -05:00
parent 00b3d0658a
commit df6c897fde

View File

@@ -104,6 +104,9 @@ enum ConstructVariant {
case EqualTo
case NotEqualTo
case LogicalAnd
case LogicalOr
// Unary operation variants
case Negation
case BitwiseCompliment
@@ -111,6 +114,10 @@ enum ConstructVariant {
// Expression variants
case TermSequence
case AdditiveExpressionSequence
case RelationalExpressionSequence
case EqualityExpressionSequence
case LogicalAndExpressionSequence
// Term variants
case FactorSequence
@@ -141,6 +148,8 @@ enum ConstructType {
case AdditionPriorityOperator
case InequalityPriorityOperator
case EqualityPriorityOperator
case LogicalAndPriorityOperator
case LogicalOrPriorityOperator
case Program
case Function
@@ -214,13 +223,55 @@ let constructDefinitions: Dictionary<ConstructType, Dictionary<ConstructVariant,
],
],
.LogicalAndPriorityOperator: [
.LogicalAnd: [
.Token(type: .LOGICAL_AND),
],
],
.LogicalOrPriorityOperator: [
.LogicalAnd: [
.Token(type: .LOGICAL_OR),
],
],
// Expressions
.Expression: [
.LogicalAndExpressionSequence: [
.Construct(type: .LogicalAndExpression),
.Loop(type: .LogicalOrPriorityOperator),
]
],
.LogicalAndExpression: [
.EqualityExpressionSequence: [
.Construct(type: .EqualityExpression),
.Loop(type: .LogicalAndPriorityOperator),
]
],
.EqualityExpression: [
.RelationalExpressionSequence: [
.Construct(type: .RelationalExpression),
.Loop(type: .EqualityPriorityOperator),
]
],
.RelationalExpression: [
.AdditiveExpressionSequence: [
.Construct(type: .AdditiveExpression),
.Loop(type: .InequalityPriorityOperator),
]
],
.AdditiveExpression: [
.TermSequence: [
.Construct(type: .Term),
.Loop(type: .AdditionPriorityOperator),
]
],
// Sub-expressions
.Term: [
.FactorSequence: [
.Construct(type: .Factor),
@@ -243,6 +294,7 @@ let constructDefinitions: Dictionary<ConstructType, Dictionary<ConstructVariant,
]
],
// Super-expressions
.Statement: [
.ReturnInteger: [
.Token(type: .RETURN),
@@ -421,6 +473,30 @@ func generateOutput(_ node: SyntaxTreeNode) -> String {
}
break
case .LogicalAndExpressionSequence:
for child in node.children {
text += generateOutput(child)
}
break
case .EqualityExpressionSequence:
for child in node.children {
text += generateOutput(child)
}
break
case .RelationalExpressionSequence:
for child in node.children {
text += generateOutput(child)
}
break
case .AdditiveExpressionSequence:
for child in node.children {
text += generateOutput(child)
}
break
case .TermSequence:
var operation: ConstructVariant = .Error
let count = node.children.count
@@ -678,49 +754,6 @@ func validateConstruct(type: ConstructType, variant: ConstructVariant, tokens: i
break
}
/*
if valid == .Invalid {
print("\(indent)Subconstruct validation failed")
return .Invalid
} else if valid == .Break {
print("\(indent)End validate subconstruct (variant \"\(validVariant) by breaking\")")
break
}
*/
//print("\(indent)End validate subconstruct (variant \"\(validVariant)\")")
break
case .Token(let type):
if let token: Token = tokens.popLast() {
if type != token.type {
print("\(indent)VALIDATION FAILED FOR TOKEN \"\(token.content)\"")
return .Invalid
}
print("\(indent)Validated token \"\(token.content)\"")
if token.type == .LITERAL_INTEGER {
node.value = String(token.content)
}
else if token.type == .IDENTIFIER {
node.value = String(token.content)
}
continue
} else {
print("\(indent)RAN OUT OF TOKENS")
return .Invalid
}
break
case .Construct(let type):
print("\(indent)Begin validate subconstruct (type \"\(type)\")")
@@ -752,12 +785,6 @@ func validateConstruct(type: ConstructType, variant: ConstructVariant, tokens: i
}
if valid == .Invalid {
print("\(indent)Subconstruct validation failed")
return .Invalid