flat assembler
Message board for the users of flat assembler.
Index
> Programming Language Design > Question about parsing if statement |
| Author |
|
|
bitRAKE 30 Oct 2025, 03:36
Fulgurance wrote: how can I know if the second if is or is not under the first if condition ? Code: # A simple evaluator for your package info file class PackageApiEvaluator # @condition_stack tracks the "truthiness" of the current block. # We start with [true] for the global scope. def initialize @condition_stack = [true] end # Returns true if the *current* block we are in is active. def active? : Bool @condition_stack.last end # Main processing loop def evaluate_file(path : String) File.each_line(path) do |line| process_line(line.strip) end # After the file, the stack should only have the global scope left if @condition_stack.size != 1 raise "Mismatched 'if'/'endif' blocks. Stack is not balanced." end end def process_line(line : String) return if line.empty? if line.starts_with?("if ") # --- Handle IF --- condition_string = line[3..] if active? # We are in an active block, so we must evaluate is_true = evaluate_condition(condition_string) @condition_stack.push(is_true) else # We are already in a "false" block. # This new 'if' is automatically skipped. # Push 'false' to maintain stack balance. @condition_stack.push(false) end elsif line == "endif" # --- Handle ENDIF --- if @condition_stack.size <= 1 raise "Unexpected 'endif' without a matching 'if'" end @condition_stack.pop else # --- Handle a normal statement --- # Only execute if our current stack state is active if active? execute_statement(line) end end end # This is where you would put your real logic def evaluate_condition(condition : String) : Bool puts " (Evaluating condition: '#{condition}')" # Example logic: case condition when "arch == x86_64" # Replace with `host_cpu.includes?("x86_64")` or similar return true when "distro == fedora" return false # Dummy value else return true # Default end end # This is where you would call your API functions def execute_statement(statement : String) puts "Executing: #{statement}" # Example: # if statement.starts_with?("Dependency") # add_dependency(statement[11..].strip) # end end end # --- Example Usage --- # Create a dummy file for the test File.write("package.info", <<-FILE Name "MyPackage" Version "1.0" if arch == x86_64 Source "source-x64.tar.gz" if distro == fedora Dependency "lib-fedora.so" endif Dependency "common-x64.so" endif if distro == ubuntu Dependency "lib-ubuntu.so" endif Description "A test package" FILE ) puts "--- Starting Evaluator ---" evaluator = PackageApiEvaluator.new evaluator.evaluate_file("package.info") puts "--- Evaluator Finished ---" Crystal seems to be an interesting language. _________________ ¯\(°_o)/¯ AI may [not] have aided with the above reply. |
|||
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.