! Breaking

This commit is contained in:
2026-01-31 15:07:04 -05:00
parent cde395a2a9
commit 2e3dd13009
8 changed files with 30 additions and 37 deletions

View File

@@ -451,27 +451,37 @@ enum Validity {
} }
func validateConstruct(_ construct: Construct, tokens: inout [Token], node: SyntaxTreeNode) -> Validity { func validateConstruct(_ construct: Construct, tokens: inout [Token], node: SyntaxTreeNode) -> Validity {
var indent: String {
var count: Int = 0
var child: SyntaxTreeNode = node
while child.parent != nil {
count += 1
child = child.parent!
}
return String(repeating: " ", count: count)
}
var loop: Bool = false var loop: Bool = false
repeat { repeat {
for element in construct { for element in construct {
switch element { switch element {
case .Construct(let type): case .Construct(let type):
print("Begin validate subconstruct (type \"\(type)\")") print("\(indent)Begin validate subconstruct (type \"\(type)\")")
var valid: Validity = .Invalid var valid: Validity = .Invalid
var validVariant: ConstructVariant = .Error var validVariant: ConstructVariant = .Error
let tokenBackup: [Token] = tokens let tokenBackup: [Token] = tokens
for key in constructDefinitions[type]!.keys { for key in constructDefinitions[type]!.keys {
let childNode = node.addChild(value: key) let childNode = node.addChild(value: key)
print("Testing variant \(key) (Loop = \(loop))") print("\(indent)Testing variant \(key) (Loop = \(loop))")
valid = validateConstruct(constructDefinitions[type]![key]!, tokens: &tokens, node: childNode) valid = validateConstruct(constructDefinitions[type]![key]!, tokens: &tokens, node: childNode)
if valid == .Invalid { if valid == .Invalid {
print("Fail") print("\(indent)Fail")
tokens = tokenBackup tokens = tokenBackup
_ = node.popLastChild() _ = node.popLastChild()
continue continue
} }
print("Success") print("\(indent)Success")
validVariant = key validVariant = key
break break
@@ -484,10 +494,10 @@ func validateConstruct(_ construct: Construct, tokens: inout [Token], node: Synt
if type == .Factor { if type == .Factor {
let nextToken: Token = tokens[tokens.count - 1] let nextToken: Token = tokens[tokens.count - 1]
if nextToken.type == .MULTIPLICATION || nextToken.type == .DIVISION { if nextToken.type == .MULTIPLICATION || nextToken.type == .DIVISION {
print("Looping due to factor") print("\(indent)Looping due to factor")
loop = true loop = true
} else { } else {
print("Breaking out of factor loop") print("\(indent)Breaking out of factor loop")
loop = false loop = false
return .Break return .Break
} }
@@ -497,10 +507,10 @@ func validateConstruct(_ construct: Construct, tokens: inout [Token], node: Synt
else if type == .Term { else if type == .Term {
let nextToken: Token = tokens[tokens.count - 1] let nextToken: Token = tokens[tokens.count - 1]
if nextToken.type == .ADDITION || nextToken.type == .NEGATION { if nextToken.type == .ADDITION || nextToken.type == .NEGATION {
print("Looping due to term") print("\(indent)Looping due to term")
loop = true loop = true
} else { } else {
print("Breaking out of term loop") print("\(indent)Breaking out of term loop")
loop = false loop = false
return .Break return .Break
} }
@@ -510,26 +520,24 @@ func validateConstruct(_ construct: Construct, tokens: inout [Token], node: Synt
if valid == .Invalid { if valid == .Invalid {
print("Subconstruct validation failed") print("\(indent)Subconstruct validation failed")
return .Invalid return .Invalid
} else if valid == .Break { } else if valid == .Break {
print("End validate subconstruct (variant \"\(validVariant) by breaking\")") print("\(indent)End validate subconstruct (variant \"\(validVariant) by breaking\")")
print(type) print(type)
break break
} }
print("End validate subconstruct (variant \"\(validVariant)\")") print("\(indent)End validate subconstruct (variant \"\(validVariant)\")")
print(type)
continue continue
case .Token(let type): case .Token(let type):
if let token: Token = tokens.popLast() { if let token: Token = tokens.popLast() {
if type != token.type { if type != token.type {
print("VALIDATION FAILED FOR TOKEN \"\(token.content)\"") print("\(indent)VALIDATION FAILED FOR TOKEN \"\(token.content)\"")
return .Invalid return .Invalid
} }
print("Validated token \"\(token.content)\"") print("\(indent)Validated token \"\(token.content)\"")
if token.type == .LITERAL_INTEGER { if token.type == .LITERAL_INTEGER {
node.value = String(token.content) node.value = String(token.content)
@@ -541,7 +549,7 @@ func validateConstruct(_ construct: Construct, tokens: inout [Token], node: Synt
continue continue
} else { } else {
print("RAN OUT OF TOKENS") print("\(indent)RAN OUT OF TOKENS")
return .Invalid return .Invalid
} }
} }

BIN
a.out

Binary file not shown.

View File

@@ -1,21 +0,0 @@
.file "bitwise.c"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $-13, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 15.2.1 20260103"
.section .note.GNU-stack,"",@progbits

BIN
my.out

Binary file not shown.

3
test.c Normal file
View File

@@ -0,0 +1,3 @@
int main() {
return !1 * 1;
}

0
test.s Normal file
View File

3
test2.c Normal file
View File

@@ -0,0 +1,3 @@
int main() {
return (~2 + !(4 - 2) / 9 - -9) + 90 * 8 - 2 - 33 * (400 - 1) - !~-9;
}

0
test2.s Normal file
View File