mirror of https://github.com/edg-l/PascalMLIR.git
tests
This commit is contained in:
parent
50ea2808af
commit
4cf7ade8e8
20
README.md
20
README.md
|
@ -1,4 +1,18 @@
|
|||
https://www.cs.utexas.edu/users/novak/iso7185.pdf
|
||||
https://lalrpop.github.io/lalrpop/lexer_tutorial/004_token_references.html
|
||||
# PascalMLIR
|
||||
|
||||
im at 6.1.7
|
||||
A Pascal compiler using LLVM MLIR in Rust.
|
||||
|
||||
Tries to comply with the [ISO/IEC 7185:1990 Standard](https://www.cs.utexas.edu/users/novak/iso7185.pdf)
|
||||
|
||||
Work in progress.
|
||||
|
||||
## TODO
|
||||
|
||||
Currently doing the parser and AST.
|
||||
|
||||
<details>
|
||||
|
||||
- [ ] Parser
|
||||
- [ ] Codegen
|
||||
|
||||
</details>
|
||||
|
|
14
src/ast.rs
14
src/ast.rs
|
@ -1,20 +1,18 @@
|
|||
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Number<'a> {
|
||||
Integer(&'a str),
|
||||
Real(&'a str)
|
||||
Real(&'a str),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Constant<'a> {
|
||||
Identifier {
|
||||
is_negative: bool,
|
||||
ident: &'a str
|
||||
},
|
||||
Identifier { is_negative: bool, ident: &'a str },
|
||||
Number(Number<'a>),
|
||||
String(&'a str),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ConstantDef<'a> {
|
||||
pub ident: &'a str,
|
||||
pub value: Constant<'a>
|
||||
pub value: Constant<'a>,
|
||||
}
|
||||
|
|
|
@ -31,17 +31,12 @@ Comma<T>: Vec<T> = {
|
|||
}
|
||||
};
|
||||
|
||||
|
||||
pub Hello: String = {
|
||||
"program" => "let".to_string()
|
||||
}
|
||||
|
||||
Number: ast::Number<'input> = {
|
||||
pub Number: ast::Number<'input> = {
|
||||
<"integer"> => ast::Number::Integer(<>),
|
||||
<"real"> => ast::Number::Real(<>),
|
||||
}
|
||||
|
||||
Constant: ast::Constant<'input> = {
|
||||
pub Constant: ast::Constant<'input> = {
|
||||
<Number> => ast::Constant::Number(<>),
|
||||
<"string"> => ast::Constant::String(<>),
|
||||
"+"? <ident:"identifier"> => ast::Constant::Identifier { is_negative: false, ident },
|
||||
|
|
60
src/main.rs
60
src/main.rs
|
@ -2,10 +2,68 @@ use lalrpop_util::lalrpop_mod;
|
|||
|
||||
lalrpop_mod!(pub grammar);
|
||||
|
||||
pub mod ast;
|
||||
pub mod lexer;
|
||||
pub mod tokens;
|
||||
pub mod ast;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{ast, grammar, lexer::Lexer};
|
||||
|
||||
#[test]
|
||||
fn parse_number() {
|
||||
#[track_caller]
|
||||
fn check(input: &str, value: ast::Number) {
|
||||
let lexer = Lexer::new(input);
|
||||
let parser = grammar::NumberParser::new();
|
||||
assert_eq!(parser.parse("", lexer).unwrap(), value)
|
||||
}
|
||||
|
||||
check("2", ast::Number::Integer("2"));
|
||||
check("+100", ast::Number::Integer("+100"));
|
||||
check("-100", ast::Number::Integer("-100"));
|
||||
check("-0.1", ast::Number::Real("-0.1"));
|
||||
check("2.2", ast::Number::Real("2.2"));
|
||||
check("1e10", ast::Number::Real("1e10"));
|
||||
check("-1e10", ast::Number::Real("-1e10"));
|
||||
check("87.35E+8", ast::Number::Real("87.35E+8"));
|
||||
check("-87.35E+8", ast::Number::Real("-87.35E+8"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_constant() {
|
||||
#[track_caller]
|
||||
fn check(input: &str, value: ast::Constant) {
|
||||
let lexer = Lexer::new(input);
|
||||
let parser = grammar::ConstantParser::new();
|
||||
assert_eq!(parser.parse("", lexer).unwrap(), value)
|
||||
}
|
||||
|
||||
check(
|
||||
"MYIDENTIFIER",
|
||||
ast::Constant::Identifier {
|
||||
is_negative: false,
|
||||
ident: "MYIDENTIFIER",
|
||||
},
|
||||
);
|
||||
check(
|
||||
"-MYIDENTIFIER",
|
||||
ast::Constant::Identifier {
|
||||
is_negative: true,
|
||||
ident: "MYIDENTIFIER",
|
||||
},
|
||||
);
|
||||
|
||||
check("2", ast::Constant::Number(ast::Number::Integer("2")));
|
||||
check("+100", ast::Constant::Number(ast::Number::Integer("+100")));
|
||||
check("-100", ast::Constant::Number(ast::Number::Integer("-100")));
|
||||
|
||||
check("-0.1", ast::Constant::Number(ast::Number::Real("-0.1")));
|
||||
check("2.2", ast::Constant::Number(ast::Number::Real("2.2")));
|
||||
check("1e10", ast::Constant::Number(ast::Number::Real("1e10")));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue