Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pointer and reference support #242

Merged
merged 19 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Cargo.lock

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

13 changes: 0 additions & 13 deletions a.out

This file was deleted.

24 changes: 24 additions & 0 deletions examples/pointer.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
TYPE MyStruct: STRUCT x: DINT; y: DINT; END_STRUCT END_TYPE

FUNCTION main : DINT
main := foo();
END_FUNCTION

FUNCTION foo : DINT
VAR
x : DINT;
s : MyStruct;
u,y : REF_TO DINT;
z : REF_TO REF_TO DINT;

END_VAR
u := NULL;
u := &s.x;
y := u;
z := &y;
s.x := 9;
z^^ := y^*2;
y^ := z^^*2;

foo := y^;
END_FUNCTION
71 changes: 56 additions & 15 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ pub enum DataType {
bounds: AstStatement,
referenced_type: Box<DataTypeDeclaration>,
},
PointerType {
name: Option<String>,
referenced_type: Box<DataTypeDeclaration>,
},
StringType {
name: Option<String>,
is_wide: bool, //WSTRING
Expand Down Expand Up @@ -349,6 +353,14 @@ impl Debug for DataType {
.field("bounds", bounds)
.field("referenced_type", referenced_type)
.finish(),
DataType::PointerType {
name,
referenced_type,
} => f
.debug_struct("PointerType")
.field("name", name)
.field("referenced_type", referenced_type)
.finish(),
DataType::StringType {
name,
is_wide,
Expand All @@ -374,6 +386,7 @@ impl DataType {
DataType::EnumType { name, elements: _ } => *name = Some(new_name),
DataType::SubRangeType { name, .. } => *name = Some(new_name),
DataType::ArrayType { name, .. } => *name = Some(new_name),
DataType::PointerType { name, .. } => *name = Some(new_name),
DataType::StringType { name, .. } => *name = Some(new_name),
DataType::VarArgs { .. } => {} //No names on varargs
}
Expand All @@ -384,6 +397,7 @@ impl DataType {
DataType::StructType { name, variables: _ } => name.as_ref().map(|x| x.as_str()),
DataType::EnumType { name, elements: _ } => name.as_ref().map(|x| x.as_str()),
DataType::ArrayType { name, .. } => name.as_ref().map(|x| x.as_str()),
DataType::PointerType { name, .. } => name.as_ref().map(|x| x.as_str()),
DataType::StringType { name, .. } => name.as_ref().map(|x| x.as_str()),
DataType::SubRangeType { name, .. } => name.as_ref().map(|x| x.as_str()),
DataType::VarArgs { .. } => None,
Expand All @@ -396,25 +410,34 @@ impl DataType {
type_name: String,
location: &SourceRange,
) -> Option<DataTypeDeclaration> {
if let DataType::ArrayType {
referenced_type, ..
} = self
{
if let DataTypeDeclaration::DataTypeReference { .. } = **referenced_type {
return None;
}
let new_data_type = DataTypeDeclaration::DataTypeReference {
referenced_type: type_name,
location: location.clone(),
};
let old_data_type = std::mem::replace(referenced_type, Box::new(new_data_type));
Some(*old_data_type)
} else {
None
match self {
DataType::ArrayType {
referenced_type, ..
} => replace_reference(referenced_type, type_name, location),
DataType::PointerType {
referenced_type, ..
} => replace_reference(referenced_type, type_name, location),
_ => None,
}
}
}

fn replace_reference(
referenced_type: &mut Box<DataTypeDeclaration>,
type_name: String,
location: &SourceRange,
) -> Option<DataTypeDeclaration> {
if let DataTypeDeclaration::DataTypeReference { .. } = **referenced_type {
return None;
}
let new_data_type = DataTypeDeclaration::DataTypeReference {
referenced_type: type_name,
location: location.clone(),
};
let old_data_type = std::mem::replace(referenced_type, Box::new(new_data_type));
Some(*old_data_type)
}

#[derive(Clone, PartialEq)]
pub struct ConditionalBlock {
pub condition: Box<AstStatement>,
Expand Down Expand Up @@ -522,6 +545,10 @@ pub enum AstStatement {
access: Box<AstStatement>,
id: AstId,
},
PointerAccess {
reference: Box<AstStatement>,
id: AstId,
},
BinaryExpression {
operator: Operator,
left: Box<AstStatement>,
Expand Down Expand Up @@ -613,12 +640,17 @@ pub enum AstStatement {
location: SourceRange,
id: AstId,
},
LiteralNull {
location: SourceRange,
id: AstId,
},
}

impl Debug for AstStatement {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match self {
AstStatement::EmptyStatement { .. } => f.debug_struct("EmptyStatement").finish(),
AstStatement::LiteralNull { .. } => f.debug_struct("LiteralNull").finish(),
AstStatement::LiteralInteger { value, .. } => f
.debug_struct("LiteralInteger")
.field("value", value)
Expand Down Expand Up @@ -806,6 +838,10 @@ impl Debug for AstStatement {
.field("reference", reference)
.field("access", access)
.finish(),
AstStatement::PointerAccess { reference, .. } => f
.debug_struct("PointerAccess")
.field("reference", reference)
.finish(),
AstStatement::MultipliedStatement {
multiplier,
element,
Expand Down Expand Up @@ -838,6 +874,7 @@ impl AstStatement {
pub fn get_location(&self) -> SourceRange {
match self {
AstStatement::EmptyStatement { location, .. } => location.clone(),
AstStatement::LiteralNull { location, .. } => location.clone(),
AstStatement::LiteralInteger { location, .. } => location.clone(),
AstStatement::LiteralDate { location, .. } => location.clone(),
AstStatement::LiteralDateAndTime { location, .. } => location.clone(),
Expand Down Expand Up @@ -900,6 +937,7 @@ impl AstStatement {
let access_loc = access.get_location();
SourceRange::new(reference_loc.range.start..access_loc.range.end)
}
AstStatement::PointerAccess { reference, .. } => reference.get_location(),
AstStatement::MultipliedStatement { location, .. } => location.clone(),
AstStatement::CaseCondition { condition, .. } => condition.get_location(),
AstStatement::ReturnStatement { location, .. } => location.clone(),
Expand All @@ -911,6 +949,7 @@ impl AstStatement {
pub fn get_id(&self) -> AstId {
match self {
AstStatement::EmptyStatement { id, .. } => *id,
AstStatement::LiteralNull { id, .. } => *id,
AstStatement::LiteralInteger { id, .. } => *id,
AstStatement::LiteralDate { id, .. } => *id,
AstStatement::LiteralDateAndTime { id, .. } => *id,
Expand All @@ -924,6 +963,7 @@ impl AstStatement {
AstStatement::QualifiedReference { id, .. } => *id,
AstStatement::Reference { id, .. } => *id,
AstStatement::ArrayAccess { id, .. } => *id,
AstStatement::PointerAccess { id, .. } => *id,
AstStatement::BinaryExpression { id, .. } => *id,
AstStatement::UnaryExpression { id, .. } => *id,
AstStatement::ExpressionList { id, .. } => *id,
Expand Down Expand Up @@ -961,6 +1001,7 @@ pub enum Operator {
And,
Or,
Xor,
Address,
}

impl Display for Operator {
Expand Down
Loading