Skip to content

Commit

Permalink
Remove constant values from ast tree
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack committed Oct 10, 2024
1 parent 3a82579 commit 22f2d87
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 178 deletions.
1 change: 0 additions & 1 deletion enderpy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ fn tokenize() -> Result<()> {

fn parse(file: &PathBuf) -> Result<()> {
let source = fs::read_to_string(file).into_diagnostic()?;
let file_path = file.to_str().unwrap_or("");
let mut parser = Parser::new(&source);
let ast = parser.parse();
println!("{:#?}", ast);
Expand Down
51 changes: 19 additions & 32 deletions parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ pub struct Node {
pub end: u32,
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] // #[serde(tag = "type")]
pub struct TextRange {
pub start: u32,
pub end: u32,
}

impl fmt::Display for Node {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}, {})", self.start, self.end)
Expand Down Expand Up @@ -342,45 +348,26 @@ pub struct Constant {
pub value: ConstantValue,
}

#[derive(Clone, PartialEq)]
#[derive(Clone, PartialEq, Debug)]
pub enum ConstantValue {
None,
Ellipsis,
Bool(bool),
Str(String),
Bytes(Vec<u8>),
Tuple(Vec<Constant>),
Bool,
Str,
Bytes,
Tuple,
// Numbers are string because we don't care about the value rn.
Int(String),
Float(String),
Complex { real: String, imaginary: String },
Int,
Float,
Complex,
}
impl fmt::Debug for ConstantValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ConstantValue::None => write!(f, "None"),
ConstantValue::Ellipsis => write!(f, "..."),
ConstantValue::Bool(b) => write!(f, "{}", b),
ConstantValue::Str(s) => {
if s.starts_with("r\"") || s.starts_with("R\"") {
let mut s = s.chars().skip(1).collect::<String>();
s = s.chars().take(s.len() - 1).collect::<String>();

s = s.chars().skip(1).collect::<String>();

return write!(f, "{:?}", s);
}

write!(f, "\"{}\"", s)
}
ConstantValue::Bytes(b) => write!(f, "{:?}", b),
ConstantValue::Tuple(t) => write!(f, "{:?}", t),
ConstantValue::Int(i) => write!(f, "{}", i),
ConstantValue::Float(fl) => write!(f, "{}", fl),
ConstantValue::Complex { real, imaginary } => write!(f, "{}+{}j", real, imaginary),
}

impl Constant {
pub fn get_value<'a>(&self, source: &'a str) -> &'a str {
return &source[self.node.start as usize..self.node.end as usize];
}
}

#[derive(Debug, Clone)]
pub struct List {
pub node: Node,
Expand Down
24 changes: 1 addition & 23 deletions parser/src/parser/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,33 +287,11 @@ impl AsPythonCompat for Name {
impl AsPythonCompat for Constant {
fn as_python_compat(&self, parser: &Parser) -> Value {
json_python_compat_node!("Constant", self, parser, {
"value": self.value.as_python_compat(parser),
"value": json!(self.get_value(parser.source)),
})
}
}

impl AsPythonCompat for ConstantValue {
fn as_python_compat(&self, parser: &Parser) -> Value {
match self {
ConstantValue::None => json!(null),
ConstantValue::Ellipsis => json!("..."),
ConstantValue::Bool(v) => json!(v),
ConstantValue::Str(v) => json!(v),
ConstantValue::Bytes(v) => json!(v),
ConstantValue::Tuple(v) => json!(v
.iter()
.map(|cons| cons.as_python_compat(parser))
.collect::<Vec<_>>()),
ConstantValue::Int(v) => Value::Number(Number::from_str(v).unwrap()),
ConstantValue::Float(v) => Value::Number(Number::from_str(v).unwrap()),
ConstantValue::Complex {
real: _real,
imaginary,
} => json!(imaginary),
}
}
}

impl AsPythonCompat for List {
fn as_python_compat(&self, parser: &Parser) -> Value {
json_python_compat_node!("List", self, parser, {
Expand Down
25 changes: 12 additions & 13 deletions parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,22 @@ pub fn concat_string_exprs(lhs: Expression, rhs: Expression) -> Result<Expressio
end: rhs.node.end,
};
let concatnated_string = match (lhs.value, rhs.value) {
(ConstantValue::Str(lhs_val), ConstantValue::Str(rhs_val)) => {
(ConstantValue::Str, ConstantValue::Str) => {
Expression::Constant(Box::new(Constant {
node,
value: ConstantValue::Str(lhs_val + &rhs_val),
value: ConstantValue::Str,
}))
}
(ConstantValue::Bytes(mut lhs), ConstantValue::Bytes(rhs)) => {
lhs.append(&mut rhs.clone());
(ConstantValue::Bytes, ConstantValue::Bytes) => {
Expression::Constant(Box::new(Constant {
node,
value: ConstantValue::Bytes(lhs),
value: ConstantValue::Bytes,
}))
}
(ConstantValue::Bytes(_lhs), _) => {
(ConstantValue::Bytes, _) => {
panic!("Cannot concat bytes and string");
}
(_, ConstantValue::Bytes(_rhs)) => {
(_, ConstantValue::Bytes) => {
panic!("Can only concat bytes with other bytes");
}
_ => panic!("Cannot concat string"),
Expand All @@ -101,13 +100,13 @@ pub fn concat_string_exprs(lhs: Expression, rhs: Expression) -> Result<Expressio
(Expression::JoinedStr(fstring_lhs), Expression::Constant(const_rhs)) => {
let mut values = fstring_lhs.values;
match const_rhs.value {
ConstantValue::Str(rhs_val) => {
ConstantValue::Str => {
values.push(Expression::Constant(Box::new(Constant {
node: const_rhs.node,
value: ConstantValue::Str(rhs_val),
value: ConstantValue::Str,
})));
}
ConstantValue::Bytes(_) => {
ConstantValue::Bytes => {
panic!("Cannot concat string and bytes");
}
_ => panic!("Cannot concat string"),
Expand All @@ -122,11 +121,11 @@ pub fn concat_string_exprs(lhs: Expression, rhs: Expression) -> Result<Expressio
}
(Expression::Constant(const_lhs), Expression::JoinedStr(fstring_rhs)) => {
let const_expr = match const_lhs.value {
ConstantValue::Str(rhs_val) => Expression::Constant(Box::new(Constant {
ConstantValue::Str => Expression::Constant(Box::new(Constant {
node: const_lhs.node,
value: ConstantValue::Str(rhs_val),
value: ConstantValue::Str,
})),
ConstantValue::Bytes(_) => {
ConstantValue::Bytes => {
panic!("Cannot concat string and bytes");
}
_ => panic!("Cannot concat string"),
Expand Down
Loading

0 comments on commit 22f2d87

Please sign in to comment.