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