adam-mcdaniel/oakc

compile time flags are constrained to the global scope

Closed this issue · 4 comments

So 'hardware' limitations force me to write the following code:

	fn run(self: &Machine) {
		let len = strlen(self->code);
		#[if (TARGET == 'm') {
			let counter = 0;
		}]
		while self->ins_ptr != len) {
			#[if (TARGET == 'm') {
                                counter = counter + 1;
				if counter > 50 {
					counter = 0;
					_sleep!();
				}
			}]
			if self->token == '+' { self.plus() }
			if self->token == '-' { self.minus() }
			if self->token == '<' { self.left() }
			if self->token == '>' { self.right() }
			if self->token == ',' { self.in() }
			if self->token == '.' { self.out() }
			if self->token == '[' { self.loop() }
			if self->token == ']' { self.end() }
			else {
				self->ins_ptr = self->ins_ptr + 1;
			}
		}
	}

But it fails:

$ cargo run -- --mar ./examples/flags/if.ok
   Compiling oakc v0.3.0 (C:\Users\Kevin\Projects\oakc)                                                                                                                                          
    Finished dev [unoptimized + debuginfo] target(s) in 2.05s
     Running `target\debug\oak.exe --mar ./examples/flags/if.ok`
  |
8 | #[if(THROW_ERROR) {
  | ^
  |
  = unexpected `#`
error: process didn't exit successfully: `target\debug\oak.exe --mar ./examples/flags/if.ok` (exit code: 1)

Is this intended?

It's not so much intended as much as it is a byproduct of the structure of HIR. Oak HIR flags are of type HirDeclaration, which constrains them to the global scope. An HirStatement::Flag variant could be added to the HirStatement enumeration which could allow conditional compilation inside function bodies.

That makes sense. Is it okay for them to be statements or does that have unwanted side effects?

I don't think so, but I could be wrong. I'm not sure I want to allow conditional compilation in a non-global scope. I think that it's better for them to remain in the global scope, but I'm not convinced of either way yet. I think it promotes hacking together clauses of a function based on the condition as opposed to writing entire implementations optimized for each condition. What do you think?

Yeah, that is a valid concern. You only want the good parts of the C preprocessor 👍 I think the duplicate code is worth forcing the programmer to be more clear with the intention of the code. Reading code is a lot harder than writing it.