Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Control Flow #152

Open
Ratstail91 opened this issue Nov 13, 2024 · 5 comments
Open

Implement Control Flow #152

Ratstail91 opened this issue Nov 13, 2024 · 5 comments
Labels
feature New feature or request TODO A planned addition

Comments

@Ratstail91
Copy link
Owner

Ratstail91 commented Nov 13, 2024

  • if-then-else
  • ternary ?:
  • while
  • do-while
  • for
  • foreach (compounds)
  • continue
  • break
  • switch-case-default (and continue?)

I'm not sure how a switch statement could be done efficiently, so it'll need research.

@Ratstail91 Ratstail91 added feature New feature or request TODO A planned addition labels Nov 13, 2024
@Ratstail91
Copy link
Owner Author

I did some thinking about this yesterday.

https://github.com/Ratstail91/Toy/blob/v2/.notes/control-flow-opcodes.txt

@Ratstail91 Ratstail91 added the in progress I am currently working on this label Nov 17, 2024
@Ratstail91
Copy link
Owner Author

Ratstail91 commented Nov 22, 2024

I started working on if-then-else a few days ago, but the tests aren't finished yet due to unrelated bug hunts.

34577ec

Edit: And finished: 0947430

@Ratstail91
Copy link
Owner Author

Ratstail91 commented Dec 2, 2024

The control flow for break and continue requires arrays to keep track during compilation, so #155 needs to be finished first.

Addendum: Turns out that wasn't the case. A bespoke "escape" structure was all that was needed.

@Ratstail91 Ratstail91 removed the in progress I am currently working on this label Dec 16, 2024
@Ratstail91 Ratstail91 added the in progress I am currently working on this label Dec 27, 2024
@Ratstail91
Copy link
Owner Author

For break/continue keywords:

instruction:
	ESCAPE opcode
	ADDR   target
	UNWIND number of scopes to unwind

continue data: (point each keyword to the innermost position)
	current depth
	list<(addr, depth)> continues

break data: (wait unto the addr is found, then go back and set the keywords' parameters)
	current depth
	list<(addr, depth)> breaks

Therefore, the following structure is needed:

struct Escape {
	unsigned int addr;
	unsigned int depth;
};

Could wrap this in a reallocate function too, similar to Toy_Array.

Ratstail91 added a commit that referenced this issue Dec 30, 2024
There were a couple bugs - I'm glad I'm so thorough with these tests.

See #152
@Ratstail91
Copy link
Owner Author

foreach isn't needed yet, and will apply to arrays and tables directly. Will add later.

do-while is rarely used, and switch isn'5 even thought out.

for might be easy, but I might start on something else, like functions, instead.

@Ratstail91 Ratstail91 removed the in progress I am currently working on this label Jan 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request TODO A planned addition
Projects
None yet
Development

No branches or pull requests

1 participant