Segfault (Self-referential struct ConstructDefinition?

This commit is contained in:
2026-01-27 08:52:11 -05:00
parent afe33e4246
commit 7051194f6b

View File

@@ -30,6 +30,7 @@ enum Element {
}
enum ConstructType {
case UnaryOperation
case Expression
case Statement
case Function
@@ -37,12 +38,15 @@ enum ConstructType {
}
enum ConstructVariant {
// Expression variants
case LiteralInteger
// Unary operation variants
case Negation
case BitwiseCompliment
case LogicalNegation
// Expression variants
case LiteralInteger
case UnaryOperation
// Statement variants
case ReturnInteger
@@ -108,24 +112,31 @@ struct ConstructDefinitions {
var variants: Dictionary<ConstructVariant, Construct>
}
let unaryOperation: ConstructDefinitions = ConstructDefinitions(
type: .UnaryOperation,
variants: [
.Negation: [
.Token(type: .NEGATION),
],
.BitwiseCompliment: [
.Token(type: .BITWISE_COMPLIMENT),
],
.LogicalNegation: [
.Token(type: .LOGICAL_NEGATION),
]
]
)
let expression: ConstructDefinitions = ConstructDefinitions(
type: .Expression,
variants: [
.LiteralInteger: [
.Token(type: .LITERAL_INTEGER)
],
.Negation: [
.Token(type: .NEGATION),
.Token(type: .LITERAL_INTEGER)
],
.BitwiseCompliment: [
.Token(type: .BITWISE_COMPLIMENT),
.Token(type: .LITERAL_INTEGER)
],
.LogicalNegation: [
.Token(type: .LOGICAL_NEGATION),
.Token(type: .LITERAL_INTEGER)
],
.UnaryOperation: [
.Construct(type: unaryOperation),
.Construct(type: expression)
]
]
)
@@ -227,7 +238,7 @@ func getTestFiles() -> [TestFile] {
var testFiles: [TestFile] = [TestFile]()
let fileManager = FileManager.default
let path = "c/tests/stage_1"
let path = "c/tests/stage_2"
do {
let validItems = try fileManager.contentsOfDirectory(atPath: path + "/valid")
@@ -334,6 +345,9 @@ func validateConstruct(_ construct: Construct, tokens: inout [Token], node: Synt
var valid: Bool = false
var validVariant: ConstructVariant = .Error
let tokenBackup: [Token] = tokens
print("Before for key in type.variats.keys")
print(type)
print("After test")
for key in type.variants.keys {
let childNode = node.addChild(value: key)
print("Testing variant \(key)")
@@ -353,7 +367,7 @@ func validateConstruct(_ construct: Construct, tokens: inout [Token], node: Synt
return false
}
print("End validate subconstruct (variant \"\(validVariant)\")")
return true
continue
case .Token(let type):
if let token: Token = tokens.popLast() {
@@ -372,21 +386,6 @@ func validateConstruct(_ construct: Construct, tokens: inout [Token], node: Synt
continue
/*switch constructType {
case .Function:
if token.type == .IDENTIFIER {
output.append(" .globl \(token.content)\n\(token.content):\n")
}
break
case .Expression:
if token.type == .LITERAL_INTEGER {
output.append(" movl $\(token.content), %eax\n ret\n")
}
break
default:
break
}*/
} else {
print("RAN OUT OF TOKENS")
return false