Add number literal building to build.rs.
This commit is contained in:
parent
17285e84eb
commit
bae2048aef
@ -1,6 +1,7 @@
|
|||||||
use crate::ast::*;
|
use crate::ast::*;
|
||||||
use crate::parser::Rule;
|
use crate::parser::Rule;
|
||||||
use pest::iterators::{Pair, Pairs};
|
use pest::iterators::{Pair, Pairs};
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
fn expect_and_use<T>(
|
fn expect_and_use<T>(
|
||||||
file_id: usize,
|
file_id: usize,
|
||||||
@ -1227,7 +1228,27 @@ fn build_literal(file_id: usize, literal_pair: Pair<Rule>) -> Literal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn build_number_literal(file_id: usize, pair: Pair<Rule>) -> Literal {
|
fn build_number_literal(file_id: usize, pair: Pair<Rule>) -> Literal {
|
||||||
todo!()
|
let inner_pair = pair.into_inner().next().unwrap();
|
||||||
|
match inner_pair.as_rule() {
|
||||||
|
Rule::DoubleLiteral => build_double_literal(file_id, inner_pair),
|
||||||
|
Rule::LongLiteral => build_long_literal(file_id, inner_pair),
|
||||||
|
Rule::IntLiteral => build_int_literal(file_id, inner_pair),
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_double_literal(file_id: usize, inner_pair: Pair<Rule>) -> Literal {
|
||||||
|
Literal::Double(f64::from_str(&inner_pair.as_str()).unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_long_literal(file_id: usize, inner_pair: Pair<Rule>) -> Literal {
|
||||||
|
let as_string = inner_pair.as_str();
|
||||||
|
let without_el = &as_string[0..(as_string.len() - 1)];
|
||||||
|
Literal::Long(i64::from_str(without_el).unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_int_literal(file_id: usize, inner_pair: Pair<Rule>) -> Literal {
|
||||||
|
Literal::Integer(i32::from_str(&inner_pair.as_str()).unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_string_literal(file_id: usize, pair: Pair<Rule>) -> Literal {
|
fn build_string_literal(file_id: usize, pair: Pair<Rule>) -> Literal {
|
||||||
@ -1265,15 +1286,16 @@ fn build_backtick_string(file_id: usize, pair: Pair<Rule>) -> Literal {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::ast::pretty_print::PrettyPrint;
|
||||||
use crate::parser::DeimosParser;
|
use crate::parser::DeimosParser;
|
||||||
use indoc::indoc;
|
use indoc::indoc;
|
||||||
|
|
||||||
fn assert_builds(src: &str) {
|
fn assert_builds(src: &str) -> CompilationUnit {
|
||||||
let parse_result = DeimosParser::parse(Rule::CompilationUnit, src);
|
let parse_result = DeimosParser::parse(Rule::CompilationUnit, src);
|
||||||
if let Err(e) = parse_result {
|
if let Err(e) = parse_result {
|
||||||
panic!("Parsing failed.\n{}", e)
|
panic!("Parsing failed.\n{}", e)
|
||||||
} else {
|
} else {
|
||||||
build_ast(0, parse_result.unwrap().next().unwrap());
|
build_ast(0, parse_result.unwrap().next().unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1283,15 +1305,30 @@ mod tests {
|
|||||||
fn main() {
|
fn main() {
|
||||||
let x = foo[a];
|
let x = foo[a];
|
||||||
}
|
}
|
||||||
"})
|
"});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn object_index_and_call() {
|
fn object_index_and_call() {
|
||||||
assert_builds(indoc! {"\
|
assert_builds(indoc! {"
|
||||||
fn main() {
|
fn main() {
|
||||||
a[b]().c()
|
a[b]().c()
|
||||||
}
|
}
|
||||||
"})
|
"});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn int_literal() {
|
||||||
|
assert_builds("fn main() { 0 }");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn long_literal() {
|
||||||
|
assert_builds("fn main() { 1L }");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn double_literal() {
|
||||||
|
assert_builds("fn main() { 1.0 }");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user