Skip to content

Commit

Permalink
Merge pull request #707 from AleoHQ/tokenizer
Browse files Browse the repository at this point in the history
Parser Rewrite
  • Loading branch information
collinc97 committed Mar 8, 2021
2 parents 9252534 + a4f2101 commit b0e3b6b
Show file tree
Hide file tree
Showing 818 changed files with 8,369 additions and 10,082 deletions.
37 changes: 17 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ members = [
"ast",
"compiler",
"gadgets",
"grammar",
"imports",
"input",
"linter",
"package",
"parser",
"state",
"synthesizer",
]
Expand Down
4 changes: 2 additions & 2 deletions asg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ version = "1.0"
version = "1.2.3"
path = "../ast"

[dependencies.leo-grammar]
[dependencies.leo-parser]
version = "1.2.3"
path = "../grammar"
path = "../parser"

[dependencies.num-bigint]
version = "0.3"
Expand Down
37 changes: 37 additions & 0 deletions asg/src/const_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,30 @@ impl ConstInt {

const_int_map!(value_negate, x, x.checked_neg()?);

const_int_map!(value_bit_negate, x, !x);

const_int_op!(to_usize, Option<usize>, x, (*x).try_into().ok());

const_int_op!(to_u128, u128, x, *x as u128);

const_int_op!(to_u64, u64, x, *x as u64);

const_int_op!(to_u32, u32, x, *x as u32);

const_int_op!(to_u16, u16, x, *x as u16);

const_int_op!(to_u8, u8, x, *x as u8);

const_int_op!(to_i128, i128, x, *x as i128);

const_int_op!(to_i64, i64, x, *x as i64);

const_int_op!(to_i32, i32, x, *x as i32);

const_int_op!(to_i16, i16, x, *x as i16);

const_int_op!(to_i8, i8, x, *x as i8);

const_int_op!(to_string, String, x, (*x).to_string());

const_int_bimap!(value_add, x, y, x.checked_add(*y)?);
Expand Down Expand Up @@ -226,6 +248,21 @@ impl ConstInt {
}
}

pub fn cast_to(&self, target: &IntegerType) -> ConstInt {
match target {
IntegerType::I8 => ConstInt::I8(self.to_i8()),
IntegerType::I16 => ConstInt::I16(self.to_i16()),
IntegerType::I32 => ConstInt::I32(self.to_i32()),
IntegerType::I64 => ConstInt::I64(self.to_i64()),
IntegerType::I128 => ConstInt::I128(self.to_i128()),
IntegerType::U8 => ConstInt::U8(self.to_u8()),
IntegerType::U16 => ConstInt::U16(self.to_u16()),
IntegerType::U32 => ConstInt::U32(self.to_u32()),
IntegerType::U64 => ConstInt::U64(self.to_u64()),
IntegerType::U128 => ConstInt::U128(self.to_u128()),
}
}

pub fn get_type<'a>(&self) -> Type<'a> {
Type::Integer(self.get_int_type())
}
Expand Down
43 changes: 36 additions & 7 deletions asg/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@
//! Errors encountered when attempting to convert to an asg from an ast.

use crate::Span;
use leo_ast::{AstError, Error as FormattedError};
use leo_grammar::ParserError;
use leo_ast::{FormattedError, LeoError};
use leo_parser::SyntaxError;

#[derive(Debug, Error)]
pub enum AsgConvertError {
#[error("{}", _0)]
AstError(#[from] AstError),

#[error("{}", _0)]
Error(#[from] FormattedError),

Expand All @@ -35,12 +32,32 @@ pub enum AsgConvertError {
InternalError(String),

#[error("{}", _0)]
ParserError(#[from] ParserError),
SyntaxError(#[from] SyntaxError),
}

impl LeoError for AsgConvertError {
fn get_path(&self) -> Option<&str> {
match self {
AsgConvertError::Error(error) => error.get_path(),
AsgConvertError::SyntaxError(error) => error.get_path(),
AsgConvertError::ImportError(error) => error.get_path(),
AsgConvertError::InternalError(_) => None,
}
}

fn set_path(&mut self, path: &str, contents: &[String]) {
match self {
AsgConvertError::Error(error) => error.set_path(path, contents),
AsgConvertError::SyntaxError(error) => error.set_path(path, contents),
AsgConvertError::ImportError(error) => error.set_path(path, contents),
AsgConvertError::InternalError(_) => {}
}
}
}

impl AsgConvertError {
fn new_from_span(message: String, span: &Span) -> Self {
AsgConvertError::Error(FormattedError::new_from_span(message, span.clone()))
AsgConvertError::Error(FormattedError::new_from_span(message, span))
}

pub fn unresolved_circuit(name: &str, span: &Span) -> Self {
Expand Down Expand Up @@ -227,6 +244,10 @@ impl AsgConvertError {
Self::new_from_span(format!("failed to parse int value '{}'", value), span)
}

pub fn unsigned_negation(span: &Span) -> Self {
Self::new_from_span("cannot negate unsigned integer".to_string(), span)
}

pub fn immutable_assignment(name: &str, span: &Span) -> Self {
Self::new_from_span(format!("illegal assignment to immutable variable '{}'", name), span)
}
Expand Down Expand Up @@ -256,6 +277,14 @@ impl AsgConvertError {
)
}

pub fn call_test_function(span: &Span) -> Self {
Self::new_from_span("cannot call test function".to_string(), span)
}

pub fn circuit_test_function(span: &Span) -> Self {
Self::new_from_span("cannot have test function as member of circuit".to_string(), span)
}

pub fn parse_index_error() -> Self {
AsgConvertError::InternalError("failed to parse index".to_string())
}
Expand Down
23 changes: 13 additions & 10 deletions asg/src/expression/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,34 +109,34 @@ impl<'a> FromAst<'a, leo_ast::CallExpression> for CallExpression<'a> {
return Err(AsgConvertError::unexpected_type(
"circuit",
type_.map(|x| x.to_string()).as_deref(),
&span,
span,
));
}
};
let circuit_name = circuit.name.borrow().name.clone();
let member = circuit.members.borrow();
let member = member
.get(&name.name)
.ok_or_else(|| AsgConvertError::unresolved_circuit_member(&circuit_name, &name.name, &span))?;
.ok_or_else(|| AsgConvertError::unresolved_circuit_member(&circuit_name, &name.name, span))?;
match member {
CircuitMember::Function(body) => {
if body.qualifier == FunctionQualifier::Static {
return Err(AsgConvertError::circuit_static_call_invalid(
&circuit_name,
&name.name,
&span,
span,
));
} else if body.qualifier == FunctionQualifier::MutSelfRef && !target.is_mut_ref() {
return Err(AsgConvertError::circuit_member_mut_call_invalid(
&circuit_name,
&name.name,
&span,
span,
));
}
(Some(target), *body)
}
CircuitMember::Variable(_) => {
return Err(AsgConvertError::circuit_variable_call(&circuit_name, &name.name, &span));
return Err(AsgConvertError::circuit_variable_call(&circuit_name, &name.name, span));
}
}
}
Expand All @@ -150,27 +150,27 @@ impl<'a> FromAst<'a, leo_ast::CallExpression> for CallExpression<'a> {
.resolve_circuit(&circuit_name.name)
.ok_or_else(|| AsgConvertError::unresolved_circuit(&circuit_name.name, &circuit_name.span))?
} else {
return Err(AsgConvertError::unexpected_type("circuit", None, &span));
return Err(AsgConvertError::unexpected_type("circuit", None, span));
};
let circuit_name = circuit.name.borrow().name.clone();

let member = circuit.members.borrow();
let member = member
.get(&name.name)
.ok_or_else(|| AsgConvertError::unresolved_circuit_member(&circuit_name, &name.name, &span))?;
.ok_or_else(|| AsgConvertError::unresolved_circuit_member(&circuit_name, &name.name, span))?;
match member {
CircuitMember::Function(body) => {
if body.qualifier != FunctionQualifier::Static {
return Err(AsgConvertError::circuit_member_call_invalid(
&circuit_name,
&name.name,
&span,
span,
));
}
(None, *body)
}
CircuitMember::Variable(_) => {
return Err(AsgConvertError::circuit_variable_call(&circuit_name, &name.name, &span));
return Err(AsgConvertError::circuit_variable_call(&circuit_name, &name.name, span));
}
}
}
Expand Down Expand Up @@ -206,12 +206,15 @@ impl<'a> FromAst<'a, leo_ast::CallExpression> for CallExpression<'a> {
let argument = argument.get().borrow();
let converted = <&Expression<'a>>::from_ast(scope, expr, Some(argument.type_.clone().partial()))?;
if argument.const_ && !converted.is_consty() {
return Err(AsgConvertError::unexpected_nonconst(&expr.span()));
return Err(AsgConvertError::unexpected_nonconst(expr.span()));
}
Ok(Cell::new(converted))
})
.collect::<Result<Vec<_>, AsgConvertError>>()?;

if function.is_test() {
return Err(AsgConvertError::call_test_function(&value.span));
}
Ok(CallExpression {
parent: Cell::new(None),
span: Some(value.span.clone()),
Expand Down
Loading

0 comments on commit b0e3b6b

Please sign in to comment.