-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from psl-lang/feature/while
while 구문 구현
- Loading branch information
Showing
7 changed files
with
85 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
use super::{Declaration, Expression, NameExpression}; | ||
use super::{Block, Declaration, Expression, NameExpression}; | ||
|
||
#[derive(Clone, Debug, Hash, PartialEq)] | ||
pub enum Statement { | ||
Declaration(Declaration), | ||
Write(WriteStatement), | ||
While(WhileStatement), | ||
Expression(Expression), | ||
} | ||
|
||
#[derive(Clone, Debug, Hash, PartialEq)] | ||
pub struct WriteStatement { | ||
pub name: NameExpression, | ||
} | ||
|
||
#[derive(Clone, Debug, Hash, PartialEq)] | ||
pub struct WhileStatement { | ||
pub condition: Expression, | ||
pub block: Block, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use crate::{ | ||
ast::WhileStatement, | ||
codegen::{ | ||
context::CodegenContext, | ||
pass::{NameResolutionContext, NameResolutionPass}, | ||
visitor::CodegenNode, | ||
}, | ||
}; | ||
|
||
impl CodegenNode for WhileStatement { | ||
fn produce_code(self, ctx: &mut CodegenContext) -> String { | ||
let mut output = String::new(); | ||
|
||
output.push_str("while ("); | ||
output.push_str(&ctx.visit(self.condition)); | ||
output.push_str(") {\n"); | ||
for statement in self.block.statements { | ||
output.push_str(&ctx.visit(statement)); | ||
} | ||
if let Some(last_expression) = self.block.last_expression { | ||
output.push_str(&format!("{};\n", ctx.visit(last_expression))); | ||
} | ||
output.push_str("}\n"); | ||
|
||
output | ||
} | ||
} | ||
|
||
impl NameResolutionPass for WhileStatement { | ||
fn resolve(&self, ctx: &mut NameResolutionContext) { | ||
let mut scope = ctx.create_subscope(); | ||
scope.visit(&self.condition); | ||
for statement in &self.block.statements { | ||
ctx.visit(statement); | ||
} | ||
ctx.visit(&self.block.last_expression) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use winnow::{ | ||
combinator::{cut_err, opt}, | ||
Located, PResult, Parser, | ||
}; | ||
|
||
use crate::{ | ||
ast::{TokenKind, WhileStatement}, | ||
syntax::{ | ||
expressions::parse_expression, | ||
fragments::{block::parse_block, separator::parse_separator}, | ||
}, | ||
}; | ||
|
||
pub fn parse_while(s: &mut Located<&str>) -> PResult<WhileStatement> { | ||
( | ||
TokenKind::KeywordWhile, | ||
opt(TokenKind::WhitespaceHorizontal), | ||
cut_err(parse_expression), | ||
opt(TokenKind::WhitespaceHorizontal), | ||
parse_block, | ||
parse_separator, | ||
) | ||
.map(|(_, _, condition, _, block, _)| WhileStatement { condition, block }) | ||
.parse_next(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters