Skip to content

Commit

Permalink
hacking
Browse files Browse the repository at this point in the history
  • Loading branch information
g-r-a-n-t committed Jun 22, 2022
1 parent e11e689 commit 637b502
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions crates/analyzer/src/traversal/const_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub(crate) fn eval_expr(
| ast::Expr::Attribute { .. }
| ast::Expr::Call { .. }
| ast::Expr::List { .. }
| ast::Expr::Repeat { .. }
| ast::Expr::Tuple { .. }
| ast::Expr::Unit => Err(not_const_error(context, expr.span)),
}
Expand Down
31 changes: 30 additions & 1 deletion crates/analyzer/src/traversal/expressions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::builtins::{ContractTypeMethod, GlobalFunction, Intrinsic, ValueMethod};
use crate::context::{AnalyzerContext, CallType, ExpressionAttributes, Location, NamedThing};
use crate::context::{AnalyzerContext, CallType, Constant, ExpressionAttributes, Location, NamedThing};
use crate::errors::{FatalError, IndexingError};
use crate::namespace::items::{Class, FunctionId, Item, TypeDef};
use crate::namespace::scopes::BlockScopeType;
Expand All @@ -19,7 +19,9 @@ use num_bigint::BigInt;
use smol_str::SmolStr;
use std::ops::RangeInclusive;
use std::str::FromStr;
use num_traits::ToPrimitive;
use vec1::Vec1;
use crate::traversal::const_expr::eval_expr;

/// Gather context information for expressions and check for type errors.
pub fn expr(
Expand All @@ -45,6 +47,7 @@ pub fn expr(
args,
} => expr_call(context, func, generic_args, args),
fe::Expr::List { elts } => expr_list(context, elts, expected_type.as_array()),
fe::Expr::Repeat { value, len } => expr_repeat(context, value, len, expected_type.as_array()),
fe::Expr::Tuple { .. } => expr_tuple(context, exp, expected_type.as_tuple()),
fe::Expr::Str(_) => expr_str(context, exp, expected_type.as_string()),
fe::Expr::Unit => Ok(ExpressionAttributes::new(Type::unit(), Location::Value)),
Expand Down Expand Up @@ -136,6 +139,32 @@ pub fn expr_list(
})
}

pub fn expr_repeat(
context: &mut dyn AnalyzerContext,
value: Node<fe::Expr>,
len: Node<fe::Expr>,
expected_type: Option<&Array>,
) -> Result<ExpressionAttributes, FatalError> {
let value = assignable_expr(context, &value, None)?;
let len = if let Constant::Int(len) = eval_expr(context, &len) {
len
} else {
panic!("shit")
};

let array_typ = Array {
size: len.to_usize().unwrap(),
inner: value.typ,
};

Ok(ExpressionAttributes {
typ: Type::Array(array_typ),
location: Location::Memory,
move_location: None,
const_value: None,
})
}

/// Gather context information for expressions and check for type errors.
///
/// Also ensures that the expression is on the stack.
Expand Down
4 changes: 2 additions & 2 deletions crates/parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ pub enum Expr {
elts: Vec<Node<Expr>>,
},
Repeat {
elt: Box<Node<Expr>>,
value: Box<Node<Expr>>,
len: Box<Node<Expr>>
},
Tuple {
Expand Down Expand Up @@ -968,7 +968,7 @@ impl fmt::Display for Expr {
write!(f, "({})", node_comma_joined(&args.kind))
}
Expr::List { elts } => write!(f, "[{}]", node_comma_joined(elts)),
Expr::Repeat { elt, len } => write!(f, "[{}; {}]", elt.kind, len.kind),
Expr::Repeat { value: elt, len } => write!(f, "[{}; {}]", elt.kind, len.kind),
Expr::Tuple { elts } => {
if elts.len() == 1 {
write!(f, "({},)", elts[0].kind)
Expand Down
2 changes: 1 addition & 1 deletion crates/parser/src/grammar/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ fn parse_list_or_repeat(par: &mut Parser) -> ParseResult<Node<Expr>> {
let len = parse_expr(par)?;
let rbracket = par.assert(TokenKind::BracketClose);
let span = lbracket.span + rbracket.span;
Ok(Node::new(Expr::Repeat {elt: Box::new(elts[0].clone()), len: Box::new(len) }, span))
Ok(Node::new(Expr::Repeat { value: Box::new(elts[0].clone()), len: Box::new(len) }, span))
} else {
panic!("shit")
}
Expand Down

0 comments on commit 637b502

Please sign in to comment.