Skip to content

Feature While Statement - #131

Merged
spatulari merged 15 commits into
azin-lang:devfrom
Gray-SS:feature/while-stmt
Aug 1, 2026
Merged

Feature While Statement#131
spatulari merged 15 commits into
azin-lang:devfrom
Gray-SS:feature/while-stmt

Conversation

@Gray-SS

@Gray-SS Gray-SS commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

It features a while statement addition into the language.

Supported Syntax:

while <condition> loop
   <body>
end 
while
  <condition>
loop
  <body>
end

Note

The second syntax is currently not supported but can be easily extended in the parsing logic via p.skipNewlines


The main changes that have been done are:

  • KwWhile token kind added and regenerated kind_string.go using stringer; tested
  • KwWhile is correctly lexed by including it in the Keywords dictionary; tested
  • Introduced WhileStmt AST statement node
  • Added semantic analysis logic for WhileStmt; tested
  • Added codegen-analysis logic for WhileStmt; tested
  • Added transpilation logic for WhileStmt; tested
  • Implemented basic body optimization (a literal copy of the loop optimization implementation)
  • Post-Update: Replaced the while...do...end syntax for while...loop...end

Code example and the transpiled version

fn mypow(a: int, b: int): int do
  var mut i = b
  var mut result = 1

  while i > 0 loop
    result = result * a
    i = i - 1
  end

  return result
end

fn main do
  var a = mypow(2, 10)
  printf("2^10 = %d\n", a)
end

And the transpiled C code:

#include <stdio.h>

int mypow(int a, int b) {
	int i = b;
	int result = 1;
	while (i > 0) {
		result = result * a;
		i = i - 1;
	}
	return result;
}

int main(void) {
	const int a = mypow(2, 10);
	printf("2^10 = %d\n", a);
	return 0;
}

Warning

When working on this feature, I noticed the parser misses two precedence level for logical and && and logical or || operators; This prevents them from being parsed as a binary expression

Related issue

None

Checklist

  • Branched off dev
  • go test ./... passes
  • go vet ./... passes
  • Code is formatted with go fmt ./...
image

@spatulari spatulari closed this Jul 31, 2026
@brysonak brysonak reopened this Aug 1, 2026

@spatulari spatulari left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@spatulari
spatulari merged commit a991f15 into azin-lang:dev Aug 1, 2026
7 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants