Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NicEastvillage committed Oct 21, 2023
1 parent 705533b commit 0024a8f
Show file tree
Hide file tree
Showing 10 changed files with 737 additions and 477 deletions.

Large diffs are not rendered by default.

257 changes: 166 additions & 91 deletions cgaal-engine/src/edg/atledg/pmoves.rs

Large diffs are not rendered by default.

333 changes: 198 additions & 135 deletions cgaal-engine/src/game_structure/lcgs/intermediate.rs

Large diffs are not rendered by default.

141 changes: 84 additions & 57 deletions cgaal-engine/src/game_structure/lcgs/relabeling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,34 +212,56 @@ fn owned_ident_relabel_error(ident: Ident) -> SpannedError {

#[cfg(test)]
mod test {
use crate::game_structure::lcgs::ast::{
BinaryOpKind, Decl, DeclKind, Expr, ExprKind, Identifier, LabelDecl,
use crate::game_structure::lcgs::relabeling::Relabeler;
use crate::game_structure::lcgs::symbol_table::SymbIdx;
use crate::game_structure::{PropIdx, INVALID_IDX};
use crate::parsing::ast::{
BinaryOpKind, Decl, DeclKind, Expr, ExprKind, Ident, OwnedIdent, RelabelCase,
};
use crate::game_structure::lcgs::ir::relabeling::Relabeler;
use crate::game_structure::lcgs::parse;
use crate::parsing::errors::ErrorLog;
use crate::parsing::lexer::Lexer;
use crate::parsing::parser::Parser;
use crate::parsing::span::Span;

/// Helper function to parse a relabelling clause
fn parse_relabelling(input: &str) -> Vec<RelabelCase> {
let errors = ErrorLog::new();
let lexer = Lexer::new(input.as_bytes(), &errors);
let mut parser = Parser::new(lexer, &errors);
parser.relabelling().expect("Invalid relabelling").1
}

/// Helper function to parse an expression
fn parse_expr(input: &str) -> Expr {
let errors = ErrorLog::new();
let lexer = Lexer::new(input.as_bytes(), &errors);
let mut parser = Parser::new(lexer, &errors);
parser.expr().expect("Invalid expression")
}

#[test]
fn test_relabeling_expr_01() {
// Check simple relabeling from one name to another
let relabeling = "[test=res]";
let relabeling = parse::relabeling().parse(relabeling.as_bytes()).unwrap();
let expr = "5 + test";
let expr = parse::expr().parse(expr.as_bytes()).unwrap();
let expr = parse_expr("5 + test");
let relabeling = parse_relabelling("[test=res]");
let relabeler = Relabeler::new(&relabeling);
let new_expr = relabeler.relabel_expr(&expr).unwrap();
let new_expr = relabeler.relabel_expr(expr).unwrap();
assert_eq!(
new_expr,
Expr {
kind: ExprKind::BinaryOp(
BinaryOpKind::Addition,
span: Span::new(0, 8),
kind: ExprKind::Binary(
BinaryOpKind::Add,
Box::new(Expr {
kind: ExprKind::Number(5),
span: Span::new(0, 1),
kind: ExprKind::Num(5),
}),
Box::new(Expr {
kind: ExprKind::OwnedIdent(Box::new(Identifier::OptionalOwner {
owner: None,
name: "res".to_string()
}))
span: Span::new(6, 9),
kind: ExprKind::OwnedIdent(OwnedIdent::new(
None,
Ident::new(Span::new(6, 9), "res".to_string())
))
})
)
}
Expand All @@ -249,22 +271,23 @@ mod test {
#[test]
fn test_relabeling_expr_02() {
// Check simple relabeling to number
let relabeling = "[test=4]";
let relabeling = parse::relabeling().parse(relabeling.as_bytes()).unwrap();
let expr = "5 + test";
let expr = parse::expr().parse(expr.as_bytes()).unwrap();
let expr = parse_expr("5 + test");
let relabeling = parse_relabelling("[test=4]");
let relabeler = Relabeler::new(&relabeling);
let new_expr = relabeler.relabel_expr(&expr).unwrap();
let new_expr = relabeler.relabel_expr(expr).unwrap();
assert_eq!(
new_expr,
Expr {
kind: ExprKind::BinaryOp(
BinaryOpKind::Addition,
span: Span::new(0, 8),
kind: ExprKind::Binary(
BinaryOpKind::Add,
Box::new(Expr {
kind: ExprKind::Number(5),
span: Span::new(0, 1),
kind: ExprKind::Num(5),
}),
Box::new(Expr {
kind: ExprKind::Number(4),
span: Span::new(6, 7),
kind: ExprKind::Num(4),
}),
)
}
Expand All @@ -274,28 +297,29 @@ mod test {
#[test]
fn test_relabeling_expr_03() {
// Check simple relabeling of identifier part
let relabeling = "[bar=yum]";
let relabeling = parse::relabeling().parse(relabeling.as_bytes()).unwrap();
let expr = "foo.bar + bar.baz";
let expr = parse::expr().parse(expr.as_bytes()).unwrap();
let expr = parse_expr("foo.bar + bar.baz");
let relabeling = parse_relabelling("[bar=yum]");
let relabeler = Relabeler::new(&relabeling);
let new_expr = relabeler.relabel_expr(&expr).unwrap();
let new_expr = relabeler.relabel_expr(expr).unwrap();
assert_eq!(
new_expr,
Expr {
kind: ExprKind::BinaryOp(
BinaryOpKind::Addition,
span: Span::new(0, 17),
kind: ExprKind::Binary(
BinaryOpKind::Add,
Box::new(Expr {
kind: ExprKind::OwnedIdent(Box::new(Identifier::OptionalOwner {
owner: Some("foo".to_string()),
name: "yum".to_string()
}))
span: Span::new(0, 7),
kind: ExprKind::OwnedIdent(OwnedIdent::new(
Some(Ident::new(Span::new(0, 3), "foo".into())),
Ident::new(Span::new(4, 7), "yum".into()),
))
}),
Box::new(Expr {
kind: ExprKind::OwnedIdent(Box::new(Identifier::OptionalOwner {
owner: Some("yum".to_string()),
name: "baz".to_string()
}))
span: Span::new(10, 17),
kind: ExprKind::OwnedIdent(OwnedIdent::new(
Some(Ident::new(Span::new(10, 13), "yum".into())),
Ident::new(Span::new(14, 17), "baz".into()),
))
}),
)
}
Expand All @@ -305,27 +329,30 @@ mod test {
#[test]
fn test_relabeling_label_01() {
// Check simple relabeling of label declaration name
let relabeling = "[foo=yum]";
let relabeling = parse::relabeling().parse(relabeling.as_bytes()).unwrap();
let decl = "label foo = bar.baz";
let decl = parse::label_decl().parse(decl.as_bytes()).unwrap();
let errors = ErrorLog::new();
let lexer = Lexer::new("label foo = bar.baz".as_bytes(), &errors);
let mut parser = Parser::new(lexer, &errors);
let decl = parser.state_label_decl().expect("Invalid expression");

let relabeling = parse_relabelling("[foo=yum]");
let relabeler = Relabeler::new(&relabeling);
let new_decl = relabeler.relabel_label(&decl).unwrap();
let new_decl = relabeler.relabel_decl(decl).unwrap();
assert_eq!(
new_decl,
Decl {
kind: DeclKind::Label(Box::new(LabelDecl {
index: 0usize,
condition: Expr {
kind: ExprKind::OwnedIdent(Box::new(Identifier::OptionalOwner {
owner: Some("bar".to_string()),
name: "baz".to_string()
}))
},
name: Identifier::Simple {
name: "yum".to_string()
}
}))
span: Span::new(0, 19),
ident: OwnedIdent::new(None, Ident::new(Span::new(6, 9), "yum".into())),
index: SymbIdx(INVALID_IDX),
kind: DeclKind::StateLabel(
PropIdx(INVALID_IDX),
Expr::new(
Span::new(12, 19),
ExprKind::OwnedIdent(OwnedIdent::new(
Some(Ident::new(Span::new(12, 15), "bar".into())),
Ident::new(Span::new(16, 19), "baz".into()),
))
)
),
}
)
}
Expand Down
13 changes: 10 additions & 3 deletions cgaal-engine/src/game_structure/lcgs/symbol_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ fn register_decls(root: LcgsRoot) -> Result<SymbolRegistry, SpannedError> {
// Constants are evaluated immediately.
// Players are put in a separate vector and handled afterwards.
// Symbol table is given ownership of the declarations.
for Decl { span, ident, kind } in root.decls {
for Decl {
span,
ident,
index: _index,
kind,
} in root.decls
{
match kind {
DeclKind::Const(expr) => {
// We can evaluate constants immediately as constants can only
Expand Down Expand Up @@ -160,9 +166,10 @@ fn check_and_optimize_decls(symbols: &SymbolTable) -> Result<(), SpannedError> {
// Optimize the declaration's expression(s)
let mut decl_ref = symbol.borrow_mut();
let Decl {
span: _,
kind,
span: _, // unchanged
ident,
index: _, // unchanged
kind,
} = decl_ref.deref_mut();
match kind {
DeclKind::StateLabel(_, expr) => {
Expand Down
3 changes: 2 additions & 1 deletion cgaal-engine/src/game_structure/lcgs/symbol_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ impl SymbolTable {
/// Creates and inserts a symbol for the given declaration under the given owned name.
/// Returns the index of the inserted symbol.
/// If the name is already associated with a different symbol, an error is returned instead.
pub fn insert(&mut self, decl: Decl) -> Result<SymbIdx, SpannedError> {
pub fn insert(&mut self, mut decl: Decl) -> Result<SymbIdx, SpannedError> {
if self.exists(&decl.ident.to_string()) {
return Err(SpannedError::new(
decl.ident.name.span,
format!("The name '{}' is already declared", decl.ident),
));
}
decl.index = SymbIdx(self.symbols.len());
self.symbols.push(Symbol::new(decl));
Ok(SymbIdx(self.symbols.len() - 1))
}
Expand Down
32 changes: 19 additions & 13 deletions cgaal-engine/src/parsing/ast.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::fmt::{Display, Formatter};
use std::ops::{Add, Div, Mul, RangeInclusive, Sub};
use crate::game_structure::lcgs::symbol_table::SymbIdx;
use crate::game_structure::{INVALID_IDX, PlayerIdx, PropIdx};
use crate::parsing::span::{NO_SPAN, Span};
use crate::game_structure::{PlayerIdx, PropIdx, INVALID_IDX};
use crate::parsing::span::{Span, NO_SPAN};
use crate::parsing::token::TokenKind;
use std::fmt::{Display, Formatter};
use std::ops::{Add, Div, Mul, RangeInclusive, Sub};

/// The root of an LCGS program.
#[derive(Debug, Eq, PartialEq, Clone)]
Expand All @@ -23,12 +23,18 @@ impl LcgsRoot {
pub struct Decl {
pub span: Span,
pub ident: OwnedIdent,
pub index: SymbIdx,
pub kind: DeclKind,
}

impl Decl {
pub fn new(span: Span, ident: Ident, kind: DeclKind) -> Self {
Decl { span, ident: OwnedIdent::new(None, ident), kind }
Decl {
span,
ident: OwnedIdent::new(None, ident),
index: SymbIdx(INVALID_IDX),
kind,
}
}

pub fn new_error() -> Self {
Expand Down Expand Up @@ -106,12 +112,7 @@ pub struct StateVarDecl {
}

impl StateVarDecl {
pub fn new(
range: RangeClause,
init: Expr,
update_ident: Ident,
update: Expr,
) -> Self {
pub fn new(range: RangeClause, init: Expr, update_ident: Ident, update: Expr) -> Self {
StateVarDecl {
range,
init,
Expand All @@ -132,7 +133,12 @@ pub struct RangeClause {

impl RangeClause {
pub fn new(span: Span, min: Expr, max: Expr) -> Self {
RangeClause { span, min, max, val: 0..=0 }
RangeClause {
span,
min,
max,
val: 0..=0,
}
}
}

Expand Down Expand Up @@ -228,7 +234,7 @@ impl From<&str> for OwnedIdent {
2 => OwnedIdent::new(
Some(Ident::new(NO_SPAN, split[0].to_string())),
Ident::new(NO_SPAN, split[1].to_string()),
),
),
_ => panic!("Invalid owned identifier. Must consist of an owner and a name."),
}
}
Expand Down
Loading

0 comments on commit 0024a8f

Please sign in to comment.