-
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.
- Loading branch information
Showing
10 changed files
with
163 additions
and
1 deletion.
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
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,70 @@ | ||
use crate::{ | ||
ast::{LiteralExpression, TokenKind}, | ||
codegen::{ | ||
construct::Type, | ||
context::CodegenContext, | ||
pass::{NameResolutionContext, NameResolutionPass}, | ||
visitor::CodegenNode, | ||
}, | ||
}; | ||
|
||
impl CodegenNode for LiteralExpression { | ||
fn produce_code(self, _ctx: &mut CodegenContext) -> String { | ||
let digit_only = self.value.content.replace('_', ""); | ||
match self.value.kind { | ||
TokenKind::LiteralDecimal => digit_only, | ||
TokenKind::LiteralHexadecimal => format!("0x{digit_only}"), | ||
TokenKind::LiteralBinary => { | ||
let mut binary = String::from("0x"); | ||
let mut buffer = 0; | ||
let mut nibble_len = digit_only.chars().count() % 4; | ||
for bit in digit_only.chars() { | ||
buffer <<= 1; | ||
if bit == '1' { | ||
buffer |= 1; | ||
} | ||
nibble_len -= 1; | ||
if nibble_len == 0 { | ||
binary.push(to_hex_digit(buffer)); | ||
} | ||
} | ||
binary | ||
} | ||
_ => unreachable!( | ||
"Invalid TokenKind for LiteralExpression: {:?}", | ||
self.value.kind | ||
), | ||
} | ||
} | ||
} | ||
impl NameResolutionPass for LiteralExpression { | ||
fn resolve(&self, _ctx: &mut NameResolutionContext) {} | ||
} | ||
|
||
impl LiteralExpression { | ||
pub fn infer_type(&self, _ctx: &CodegenContext) -> Result<Type, String> { | ||
Ok(Type::Integer) | ||
} | ||
} | ||
|
||
fn to_hex_digit(number: i32) -> char { | ||
match number { | ||
0 => '0', | ||
1 => '1', | ||
2 => '2', | ||
3 => '3', | ||
4 => '4', | ||
5 => '5', | ||
6 => '6', | ||
7 => '7', | ||
8 => '8', | ||
9 => '9', | ||
10 => 'A', | ||
11 => 'B', | ||
12 => 'C', | ||
13 => 'D', | ||
14 => 'E', | ||
15 => 'F', | ||
_ => unreachable!("No matching hex digit for {number}"), | ||
} | ||
} | ||
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,13 @@ | ||
use winnow::{combinator::alt, Located, PResult, Parser}; | ||
|
||
use crate::ast::{LiteralExpression, TokenKind}; | ||
|
||
pub fn parse_literal(s: &mut Located<&str>) -> PResult<LiteralExpression> { | ||
alt(( | ||
TokenKind::LiteralDecimal, | ||
TokenKind::LiteralHexadecimal, | ||
TokenKind::LiteralBinary, | ||
)) | ||
.map(|token| LiteralExpression { value: token }) | ||
.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
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,44 @@ | ||
use winnow::{ | ||
combinator::{alt, preceded}, | ||
token::take_while, | ||
Located, PResult, Parser, | ||
}; | ||
|
||
use crate::ast::{Token, TokenKind}; | ||
|
||
use super::token; | ||
|
||
pub fn parse_literal(s: &mut Located<&str>) -> PResult<Token> { | ||
parse_integer_literal.parse_next(s) | ||
} | ||
|
||
pub fn parse_integer_literal(s: &mut Located<&str>) -> PResult<Token> { | ||
alt((parse_hexadecimal, parse_binary, parse_decimal)).parse_next(s) | ||
} | ||
|
||
pub fn parse_decimal(s: &mut Located<&str>) -> PResult<Token> { | ||
take_while(1.., ('0'..='9', '_')) | ||
.verify(|s: &str| s.chars().any(|c| c != '_')) | ||
.with_span() | ||
.map(token(TokenKind::LiteralDecimal)) | ||
.parse_next(s) | ||
} | ||
|
||
pub fn parse_hexadecimal(s: &mut Located<&str>) -> PResult<Token> { | ||
preceded( | ||
"0x", | ||
take_while(1.., ('0'..='9', 'a'..='f', 'A'..='F', '_')), | ||
) | ||
.verify(|s: &str| s.chars().any(|c| c != '_')) | ||
.with_span() | ||
.map(token(TokenKind::LiteralHexadecimal)) | ||
.parse_next(s) | ||
} | ||
|
||
pub fn parse_binary(s: &mut Located<&str>) -> PResult<Token> { | ||
preceded("0b", take_while(1.., ('0'..='1', '_'))) | ||
.verify(|s: &str| s.chars().any(|c| c != '_')) | ||
.with_span() | ||
.map(token(TokenKind::LiteralBinary)) | ||
.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
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,16 @@ | ||
i32 year = read i32 | ||
i32 is_leap = if year % 4 == 0 { | ||
if year % 400 == 0 { | ||
1 | ||
} else { | ||
if year % 100 == 0 { | ||
0 | ||
} else { | ||
1 | ||
} | ||
} | ||
} else { | ||
0 | ||
} | ||
|
||
write is_leap |