Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
Close #421 (#432)
Browse files Browse the repository at this point in the history
`find src/ -name "*.rs" | xargs sed -i '' 's/parse_expr/expr/' && cargo fmt`
  • Loading branch information
hdamron17 authored May 13, 2020
1 parent a8159c3 commit daf0726
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 61 deletions.
82 changes: 41 additions & 41 deletions src/analyze/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::intern::InternedStr;
use crate::parse::Lexer;

impl<T: Lexer> Analyzer<T> {
pub fn parse_expr(&mut self, expr: ast::Expr) -> Expr {
pub fn expr(&mut self, expr: ast::Expr) -> Expr {
use ast::ExprType::*;

let _guard = self.recursion_check();
Expand Down Expand Up @@ -42,20 +42,20 @@ impl<T: Lexer> Analyzer<T> {
Div(left, right) => self.binary_helper(left, right, BinaryOp::Div, Self::mul),
Mod(left, right) => self.binary_helper(left, right, BinaryOp::Mod, Self::mul),
Assign(lval, rval, token) => {
let lval = self.parse_expr(*lval);
let rval = self.parse_expr(*rval);
let lval = self.expr(*lval);
let rval = self.expr(*rval);
self.assignment_expr(lval, rval, token, expr.location)
}
Add(left, right) => self.binary_helper(left, right, BinaryOp::Add, Self::add),
Sub(left, right) => self.binary_helper(left, right, BinaryOp::Sub, Self::add),
FuncCall(func, args) => self.func_call(*func, args),
Member(struct_, id) => {
let struct_ = self.parse_expr(*struct_);
let struct_ = self.expr(*struct_);
self.struct_member(struct_, id, expr.location)
}
// s->p desguars to (*s).p
DerefMember(inner, id) => {
let inner = self.parse_expr(*inner);
let inner = self.expr(*inner);
let struct_type = match &inner.ctype {
Type::Pointer(ctype, _) => match &**ctype {
Type::Union(_) | Type::Struct(_) => (**ctype).clone(),
Expand All @@ -77,7 +77,7 @@ impl<T: Lexer> Analyzer<T> {
}
// `*p` or `a[i]`
Deref(inner) => {
let inner = self.parse_expr(*inner);
let inner = self.expr(*inner);
match &inner.ctype {
Type::Array(t, _) | Type::Pointer(t, _) => {
let ctype = (**t).clone();
Expand All @@ -95,7 +95,7 @@ impl<T: Lexer> Analyzer<T> {
// &x
// 6.5.3.2 Address and indirection operators
AddressOf(inner) => {
let inner = self.parse_expr(*inner);
let inner = self.expr(*inner);
match inner.expr {
// parse &*x as x
// footnote 102: &*E is equivalent to E (even if E is a null pointer)
Expand Down Expand Up @@ -141,15 +141,15 @@ impl<T: Lexer> Analyzer<T> {
self.align(ctype, expr.location)
}
AlignofExpr(inner) => {
let inner = self.parse_expr(*inner);
let inner = self.expr(*inner);
self.align(inner.ctype, expr.location)
}
SizeofType(type_name) => {
let ctype = self.parse_typename(type_name, expr.location);
self.sizeof(ctype, expr.location)
}
SizeofExpr(inner) => {
let inner = self.parse_expr(*inner);
let inner = self.expr(*inner);
self.sizeof(inner.ctype, expr.location)
}
BitwiseNot(inner) => self.bitwise_not(*inner),
Expand All @@ -171,8 +171,8 @@ impl<T: Lexer> Analyzer<T> {
// `for(j = i, k = 0; k < n; j++, k++);`
// see also https://stackoverflow.com/a/43561555/7669110
Comma(left, right) => {
let left = self.parse_expr(*left);
let right = self.parse_expr(*right).rval();
let left = self.expr(*left);
let right = self.expr(*right).rval();
Expr {
ctype: right.ctype.clone(),
lval: false,
Expand All @@ -185,7 +185,7 @@ impl<T: Lexer> Analyzer<T> {
}
}
}
// only meant for use with `parse_expr`
// only meant for use with `expr`
// TODO: change ast::Expr to use `ExprType::Binary` as well, which would make this unnecessary
// TODO: these functions should have the locations of the parent expression, not the children
#[allow(clippy::boxed_local)]
Expand All @@ -199,8 +199,8 @@ impl<T: Lexer> Analyzer<T> {
where
F: FnOnce(&mut Self, Expr, Expr, BinaryOp) -> Expr,
{
let left = self.parse_expr(*left);
let right = self.parse_expr(*right);
let left = self.expr(*left);
let right = self.expr(*right);
expr_checker(self, left, right, op)
}
// left OP right, where OP is an operation that requires integral types
Expand Down Expand Up @@ -273,8 +273,8 @@ impl<T: Lexer> Analyzer<T> {
token: ComparisonToken,
) -> Expr {
let location = left.location.merge(right.location);
let mut left = self.parse_expr(left);
let mut right = self.parse_expr(right);
let mut left = self.expr(left);
let mut right = self.expr(right);

// i == i
if left.ctype.is_arithmetic() && right.ctype.is_arithmetic() {
Expand Down Expand Up @@ -401,7 +401,7 @@ impl<T: Lexer> Analyzer<T> {
// 6.5.4 Cast operators
fn explicit_cast(&mut self, expr: ast::Expr, ctype: Type) -> Expr {
let location = expr.location;
let expr = self.parse_expr(expr).rval();
let expr = self.expr(expr).rval();
// (void)0;
if ctype == Type::Void {
// casting anything to void is allowed
Expand Down Expand Up @@ -487,7 +487,7 @@ impl<T: Lexer> Analyzer<T> {
// `func(args)`
// 6.5.2.2 Function calls
fn func_call(&mut self, func: ast::Expr, args: Vec<ast::Expr>) -> Expr {
let mut func = self.parse_expr(func);
let mut func = self.expr(func);
// if fp is a function pointer, fp() desugars to (*fp)()
match &func.ctype {
Type::Pointer(pointee, _) if pointee.is_function() => {
Expand Down Expand Up @@ -525,7 +525,7 @@ impl<T: Lexer> Analyzer<T> {
}
let mut promoted_args = vec![];
for (i, arg) in args.into_iter().enumerate() {
let arg = self.parse_expr(arg);
let arg = self.expr(arg);
let promoted = match functype.params.get(i) {
// int f(int); f(1)
Some(expected) => arg
Expand Down Expand Up @@ -595,7 +595,7 @@ impl<T: Lexer> Analyzer<T> {
) -> Expr {
use crate::data::lex::AssignmentToken;

let expr = self.parse_expr(expr);
let expr = self.expr(expr);
if let Err(err) = expr.modifiable_lval() {
self.err(err, location);
} else if !(expr.ctype.is_arithmetic() || expr.ctype.is_pointer()) {
Expand Down Expand Up @@ -635,8 +635,8 @@ impl<T: Lexer> Analyzer<T> {
// a[i] desugars to *(a + i)
// 6.5.2.1 Array subscripting
fn index(&mut self, left: ast::Expr, right: ast::Expr, location: Location) -> Expr {
let left = self.parse_expr(left).rval();
let right = self.parse_expr(right).rval();
let left = self.expr(left).rval();
let right = self.expr(right).rval();

let (target_type, array, index) = match (&left.ctype, &right.ctype) {
// p[i]
Expand Down Expand Up @@ -674,7 +674,7 @@ impl<T: Lexer> Analyzer<T> {
// ~expr
// 6.5.3.3 Unary arithmetic operators
fn bitwise_not(&mut self, expr: ast::Expr) -> Expr {
let expr = self.parse_expr(expr);
let expr = self.expr(expr);
if !expr.ctype.is_integral() {
self.err(
SemanticError::NonIntegralExpr(expr.ctype.clone()),
Expand All @@ -694,7 +694,7 @@ impl<T: Lexer> Analyzer<T> {
// -x and +x
// 6.5.3.3 Unary arithmetic operators
fn unary_add(&mut self, expr: ast::Expr, add: bool, location: Location) -> Expr {
let expr = self.parse_expr(expr);
let expr = self.expr(expr);
if !expr.ctype.is_arithmetic() {
self.err(SemanticError::NotArithmetic(expr.ctype.clone()), location);
return expr;
Expand All @@ -719,7 +719,7 @@ impl<T: Lexer> Analyzer<T> {
// 6.5.3.3 Unary arithmetic operators
// > The expression !E is equivalent to (0==E).
fn logical_not(&mut self, expr: ast::Expr) -> Expr {
let expr = self.parse_expr(expr);
let expr = self.expr(expr);
let boolean = expr.truthy(&mut self.error_handler);
debug_assert_eq!(boolean.ctype, Type::Bool);
let zero = Expr::zero(boolean.location).implicit_cast(&Type::Bool, &mut self.error_handler);
Expand Down Expand Up @@ -757,9 +757,9 @@ impl<T: Lexer> Analyzer<T> {
otherwise: ast::Expr,
location: Location,
) -> Expr {
let condition = self.parse_expr(condition).truthy(&mut self.error_handler);
let mut then = self.parse_expr(then).rval();
let mut otherwise = self.parse_expr(otherwise).rval();
let condition = self.expr(condition).truthy(&mut self.error_handler);
let mut then = self.expr(then).rval();
let mut otherwise = self.expr(otherwise).rval();

if then.ctype.is_arithmetic() && otherwise.ctype.is_arithmetic() {
let (tmp1, tmp2) = Expr::binary_promote(then, otherwise, &mut self.error_handler);
Expand Down Expand Up @@ -1330,8 +1330,8 @@ mod test {
use super::*;
use crate::analyze::test::analyze;
use crate::analyze::*;
pub(crate) fn parse_expr(input: &str) -> CompileResult<Expr> {
analyze(input, Parser::expr, Analyzer::parse_expr)
pub(crate) fn expr(input: &str) -> CompileResult<Expr> {
analyze(input, Parser::expr, Analyzer::expr)
}
fn get_location(r: &CompileResult<Expr>) -> Location {
match r {
Expand All @@ -1340,29 +1340,29 @@ mod test {
}
}
fn assert_literal(token: Literal) {
let parsed = parse_expr(&token.to_string());
let parsed = expr(&token.to_string());
let location = get_location(&parsed);
assert_eq!(parsed.unwrap(), literal(token, location));
}
fn parse_expr_with_scope<'a>(input: &'a str, variables: &[Symbol]) -> CompileResult<Expr> {
fn expr_with_scope<'a>(input: &'a str, variables: &[Symbol]) -> CompileResult<Expr> {
analyze(input, Parser::expr, |a, expr| {
for &meta in variables {
let id = meta.get().id;
a.scope.insert(id, meta);
}
a.parse_expr(expr)
a.expr(expr)
})
}
fn assert_type(input: &str, ctype: Type) {
match parse_expr(input) {
match expr(input) {
Ok(expr) => assert_eq!(expr.ctype, ctype),
Err(err) => panic!("error: {}", err.data),
};
}
#[test]
fn test_primaries() {
assert_literal(Literal::Int(141));
let parsed = parse_expr("\"hi there\"");
let parsed = expr("\"hi there\"");

assert_eq!(
parsed,
Expand All @@ -1372,7 +1372,7 @@ mod test {
)),
);
assert_literal(Literal::Float(1.5));
let parsed = parse_expr("(1)");
let parsed = expr("(1)");
assert_eq!(parsed, Ok(literal(Literal::Int(1), get_location(&parsed))));
let x = Variable {
ctype: Type::Int(true),
Expand All @@ -1381,7 +1381,7 @@ mod test {
storage_class: Default::default(),
}
.insert();
let parsed = parse_expr_with_scope("x", &[x]);
let parsed = expr_with_scope("x", &[x]);
assert_eq!(
parsed,
Ok(Expr {
Expand Down Expand Up @@ -1417,8 +1417,8 @@ mod test {
}),
}
.insert();
assert!(parse_expr_with_scope("f(1,2,3)", &[f]).is_err());
let parsed = parse_expr_with_scope("f()", &[f]);
assert!(expr_with_scope("f(1,2,3)", &[f]).is_err());
let parsed = expr_with_scope("f()", &[f]);
assert!(match parsed {
Ok(Expr {
expr: ExprType::FuncCall(_, _),
Expand All @@ -1429,7 +1429,7 @@ mod test {
}
#[test]
fn test_type_errors() {
assert!(parse_expr("1 % 2.0").is_err());
assert!(expr("1 % 2.0").is_err());
}

#[test]
Expand All @@ -1438,7 +1438,7 @@ mod test {
assert_type("(unsigned int)4.2", Type::Int(false));
assert_type("(float)4.2", Type::Float);
assert_type("(double)4.2", Type::Double);
assert!(parse_expr("(int*)4.2").is_err());
assert!(expr("(int*)4.2").is_err());
assert_type(
"(int*)(int)4.2",
Type::Pointer(Box::new(Type::Int(true)), Qualifiers::default()),
Expand Down
4 changes: 2 additions & 2 deletions src/analyze/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl<T: Lexer> Analyzer<T> {
// initializer_list
let mut expr = match init {
Aggregate(list) => return self.check_aggregate_overflow(list, ctype, location),
Scalar(expr) => self.parse_expr(*expr),
Scalar(expr) => self.expr(*expr),
};
// The only time (that I know of) that an expression will initialize a non-scalar
// is for character literals.
Expand Down Expand Up @@ -110,7 +110,7 @@ impl<T: Lexer> Analyzer<T> {
} else {
let expr = match list.next() {
Some(Scalar(expr)) => self
.parse_expr(*expr)
.expr(*expr)
.rval()
.implicit_cast(&inner, &mut self.error_handler),
_ => unreachable!(),
Expand Down
8 changes: 4 additions & 4 deletions src/analyze/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ impl<I: Lexer> Analyzer<I> {
};
// struct s { int i: 5 };
if let Some(bitfield) = bitfield {
let bit_size = match Self::const_uint(self.parse_expr(bitfield)) {
let bit_size = match Self::const_uint(self.expr(bitfield)) {
Ok(e) => e,
Err(err) => {
self.error_handler.push_back(err);
Expand Down Expand Up @@ -701,7 +701,7 @@ impl<I: Lexer> Analyzer<I> {
for (name, maybe_value) in ast_members {
// enum E { A = 5 };
if let Some(value) = maybe_value {
discriminant = Self::const_sint(self.parse_expr(value)).unwrap_or_else(|err| {
discriminant = Self::const_sint(self.expr(value)).unwrap_or_else(|err| {
self.error_handler.push_back(err);
std::i64::MIN
});
Expand Down Expand Up @@ -850,7 +850,7 @@ impl<I: Lexer> Analyzer<I> {
Array { of, size } => {
// int a[5]
let size = if let Some(expr) = size {
let size = Self::const_uint(self.parse_expr(*expr)).unwrap_or_else(|err| {
let size = Self::const_uint(self.expr(*expr)).unwrap_or_else(|err| {
self.error_handler.push_back(err);
1
});
Expand Down Expand Up @@ -1380,7 +1380,7 @@ pub(crate) mod test {
}

pub(crate) fn analyze_expr(s: &str) -> CompileResult<Expr> {
analyze(s, Parser::expr, Analyzer::parse_expr)
analyze(s, Parser::expr, Analyzer::expr)
}

pub(crate) fn assert_decl_display(left: &str, right: &str) {
Expand Down
Loading

0 comments on commit daf0726

Please sign in to comment.