Skip to content

Commit 4a5aff8

Browse files
committed
refactor: self-review
1 parent f08c2db commit 4a5aff8

File tree

20 files changed

+375
-439
lines changed

20 files changed

+375
-439
lines changed

.vscode/launch.json

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,29 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7-
{
8-
"type": "codelldb",
9-
"request": "launch",
10-
"name": "Debug codelldb standard_lib",
11-
"cargo": {
12-
"args": [
13-
"build",
14-
"--bin=plc",
15-
"--package=plc_driver"
16-
],
17-
"filter": {
18-
"name": "plc",
19-
"kind": "bin"
20-
}
21-
},
22-
"program": "target/debug/plc",
7+
{
8+
"type": "codelldb",
9+
"request": "launch",
10+
"name": "Debug codelldb standard_lib",
11+
"cargo": {
2312
"args": [
24-
"libs/stdlib/iec61131-st/*.st"
13+
"build",
14+
"--bin=plc",
15+
"--package=plc_driver"
2516
],
26-
"cwd": "${workspaceFolder}",
27-
"env": {
28-
"RUST_LOG": "rusty"
29-
},
30-
"terminal": "integrated"
17+
"filter": {
18+
"name": "plc",
19+
"kind": "bin"
20+
}
21+
},
22+
"program": "target/debug/plc",
23+
"args": ["libs/stdlib/iec61131-st/*.st"],
24+
"cwd": "${workspaceFolder}",
25+
"env": {
26+
"RUST_LOG": "rusty"
3127
},
28+
"terminal": "integrated"
29+
},
3230
{
3331
"type": "codelldb",
3432
"request": "launch",
@@ -45,11 +43,7 @@
4543
}
4644
},
4745
"program": "target/debug/plc",
48-
"args": [
49-
"target/demo.st",
50-
"-g",
51-
"-c"
52-
],
46+
"args": ["target/demo.st", "-g", "-c"],
5347
"cwd": "${workspaceFolder}",
5448
"env": {
5549
"RUST_LOG": "rusty"
@@ -64,7 +58,7 @@
6458
"args": [
6559
"build",
6660
"--bin=plc",
67-
"--package=plc_driver",
61+
"--package=plc_driver"
6862
],
6963
"filter": {
7064
"name": "plc",
@@ -73,8 +67,7 @@
7367
},
7468
"args": [
7569
"target/demo.st",
76-
"tests/lit/util/printf.pli",
77-
"-j=1",
70+
"tests/lit/util/printf.pli"
7871
],
7972
"cwd": "${workspaceFolder}",
8073
"env": {
@@ -114,6 +107,7 @@
114107
"cwd": "${workspaceFolder}",
115108
"program": "target/demo",
116109
"terminal": "integrated"
110+
117111
}
118112
]
119-
}
113+
}

Makefile

Lines changed: 0 additions & 7 deletions
This file was deleted.

compiler/plc_ast/src/ast.rs

Lines changed: 3 additions & 267 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
literals::{AstLiteral, StringValue},
1717
pre_processor,
1818
provider::IdProvider,
19-
visitor::{AstVisitor, Walker},
19+
ser::AstSerializer,
2020
};
2121

2222
use plc_source::source_location::*;
@@ -1478,240 +1478,7 @@ impl AstNode {
14781478
}
14791479

14801480
pub fn as_string(&self) -> String {
1481-
let mut formatter = StringFormatter { result: String::new() };
1482-
formatter.visit(self);
1483-
formatter.result
1484-
}
1485-
1486-
pub fn as_string_mut(&mut self) -> String {
1487-
let mut formatter = StringFormatter { result: String::new() };
1488-
formatter.visit(self);
1489-
formatter.result
1490-
}
1491-
}
1492-
1493-
// TODO: Move into own file, rename to AstSerializer and implement some tests
1494-
struct StringFormatter {
1495-
result: String,
1496-
}
1497-
1498-
impl AstVisitor for StringFormatter {
1499-
fn visit(&mut self, node: &AstNode) {
1500-
node.walk(self)
1501-
}
1502-
1503-
fn visit_compilation_unit(&mut self, _: &CompilationUnit) {
1504-
unimplemented!("for now only interested in individual nodes located in a POU body")
1505-
}
1506-
1507-
fn visit_implementation(&mut self, _: &Implementation) {
1508-
unimplemented!("for now only interested in individual nodes located in a POU body")
1509-
}
1510-
1511-
fn visit_variable_block(&mut self, _: &VariableBlock) {
1512-
unimplemented!("for now only interested in individual nodes located in a POU body")
1513-
}
1514-
1515-
fn visit_variable(&mut self, _: &Variable) {
1516-
unimplemented!("for now only interested in individual nodes located in a POU body")
1517-
}
1518-
1519-
fn visit_config_variable(&mut self, _: &ConfigVariable) {
1520-
unimplemented!("for now only interested in individual nodes located in a POU body")
1521-
}
1522-
1523-
fn visit_interface(&mut self, _: &Interface) {
1524-
unimplemented!("for now only interested in individual nodes located in a POU body")
1525-
}
1526-
1527-
fn visit_property(&mut self, _: &PropertyBlock) {
1528-
unimplemented!("for now only interested in individual nodes located in a POU body")
1529-
}
1530-
1531-
fn visit_enum_element(&mut self, element: &AstNode) {
1532-
element.walk(self);
1533-
}
1534-
1535-
fn visit_data_type_declaration(&mut self, _: &DataTypeDeclaration) {
1536-
unimplemented!("for now only interested in individual nodes located in a POU body")
1537-
}
1538-
1539-
fn visit_user_type_declaration(&mut self, _: &UserTypeDeclaration) {
1540-
unimplemented!("for now only interested in individual nodes located in a POU body")
1541-
}
1542-
1543-
fn visit_data_type(&mut self, _: &DataType) {
1544-
unimplemented!("for now only interested in individual nodes located in a POU body")
1545-
}
1546-
1547-
fn visit_pou(&mut self, _: &Pou) {
1548-
unimplemented!("for now only interested in individual nodes located in a POU body")
1549-
}
1550-
1551-
fn visit_empty_statement(&mut self, _stmt: &EmptyStatement, _node: &AstNode) {}
1552-
1553-
fn visit_default_value(&mut self, _stmt: &DefaultValue, _node: &AstNode) {}
1554-
1555-
fn visit_literal(&mut self, stmt: &AstLiteral, _node: &AstNode) {
1556-
use crate::literals::AstLiteral;
1557-
match stmt {
1558-
AstLiteral::Integer(value) => self.result.push_str(&value.to_string()),
1559-
AstLiteral::Real(value) => self.result.push_str(value),
1560-
AstLiteral::Bool(value) => self.result.push_str(&value.to_string().to_uppercase()),
1561-
AstLiteral::String(string_value) => {
1562-
if string_value.is_wide {
1563-
self.result.push_str(&format!("\"{}\"", string_value.value));
1564-
} else {
1565-
self.result.push_str(&format!("'{}'", string_value.value));
1566-
}
1567-
}
1568-
AstLiteral::Null => self.result.push_str("NULL"),
1569-
_ => stmt.walk(self), // Let other literals use their default walking behavior
1570-
}
1571-
}
1572-
1573-
fn visit_multiplied_statement(&mut self, stmt: &MultipliedStatement, _node: &AstNode) {
1574-
stmt.walk(self)
1575-
}
1576-
1577-
fn visit_reference_expr(&mut self, stmt: &ReferenceExpr, _node: &AstNode) {
1578-
if let Some(base) = &stmt.base {
1579-
base.walk(self);
1580-
}
1581-
1582-
match &stmt.access {
1583-
ReferenceAccess::Global(reference) => {
1584-
self.result.push('.');
1585-
reference.walk(self);
1586-
}
1587-
ReferenceAccess::Member(reference) => {
1588-
if stmt.base.is_some() {
1589-
self.result.push('.');
1590-
}
1591-
reference.walk(self);
1592-
}
1593-
ReferenceAccess::Index(index) => {
1594-
self.result.push('[');
1595-
index.walk(self);
1596-
self.result.push(']');
1597-
}
1598-
ReferenceAccess::Cast(reference) => {
1599-
self.result.push('#');
1600-
reference.walk(self);
1601-
}
1602-
ReferenceAccess::Deref => {
1603-
self.result.push('^');
1604-
}
1605-
ReferenceAccess::Address => {
1606-
self.result.insert_str(0, "ADR(");
1607-
self.result.push(')');
1608-
}
1609-
}
1610-
}
1611-
1612-
fn visit_identifier(&mut self, stmt: &str, _node: &AstNode) {
1613-
self.result.push_str(stmt);
1614-
}
1615-
1616-
fn visit_direct_access(&mut self, stmt: &DirectAccess, _node: &AstNode) {
1617-
stmt.walk(self)
1618-
}
1619-
1620-
fn visit_hardware_access(&mut self, stmt: &HardwareAccess, _node: &AstNode) {
1621-
stmt.walk(self)
1622-
}
1623-
1624-
fn visit_binary_expression(&mut self, stmt: &BinaryExpression, _node: &AstNode) {
1625-
stmt.left.walk(self);
1626-
self.result.push(' ');
1627-
self.result.push_str(&stmt.operator.to_string());
1628-
self.result.push(' ');
1629-
stmt.right.walk(self);
1630-
}
1631-
1632-
fn visit_unary_expression(&mut self, stmt: &UnaryExpression, _node: &AstNode) {
1633-
self.result.push_str(&stmt.operator.to_string());
1634-
stmt.value.walk(self);
1635-
}
1636-
1637-
fn visit_expression_list(&mut self, stmt: &Vec<AstNode>, _node: &AstNode) {
1638-
for (i, node) in stmt.iter().enumerate() {
1639-
if i > 0 {
1640-
self.result.push_str(", ");
1641-
}
1642-
node.walk(self);
1643-
}
1644-
}
1645-
1646-
fn visit_paren_expression(&mut self, inner: &AstNode, _node: &AstNode) {
1647-
self.result.push('(');
1648-
inner.walk(self);
1649-
self.result.push(')');
1650-
}
1651-
1652-
fn visit_range_statement(&mut self, stmt: &RangeStatement, _node: &AstNode) {
1653-
stmt.walk(self)
1654-
}
1655-
1656-
fn visit_vla_range_statement(&mut self, _node: &AstNode) {}
1657-
1658-
fn visit_assignment(&mut self, stmt: &Assignment, _node: &AstNode) {
1659-
stmt.left.walk(self);
1660-
self.result.push_str(" := ");
1661-
stmt.right.walk(self);
1662-
}
1663-
1664-
fn visit_output_assignment(&mut self, stmt: &Assignment, _node: &AstNode) {
1665-
stmt.left.walk(self);
1666-
self.result.push_str(" => ");
1667-
stmt.right.walk(self);
1668-
}
1669-
1670-
fn visit_ref_assignment(&mut self, stmt: &Assignment, _node: &AstNode) {
1671-
stmt.left.walk(self);
1672-
self.result.push_str(" REF= ");
1673-
stmt.right.walk(self);
1674-
}
1675-
1676-
fn visit_call_statement(&mut self, stmt: &CallStatement, _node: &AstNode) {
1677-
stmt.operator.walk(self);
1678-
self.result.push('(');
1679-
if let Some(opt) = stmt.parameters.as_ref() {
1680-
opt.walk(self)
1681-
}
1682-
self.result.push(')');
1683-
}
1684-
1685-
fn visit_control_statement(&mut self, stmt: &AstControlStatement, _node: &AstNode) {
1686-
stmt.walk(self)
1687-
}
1688-
1689-
fn visit_case_condition(&mut self, child: &AstNode, _node: &AstNode) {
1690-
child.walk(self)
1691-
}
1692-
1693-
fn visit_exit_statement(&mut self, _node: &AstNode) {}
1694-
1695-
fn visit_continue_statement(&mut self, _node: &AstNode) {}
1696-
1697-
fn visit_return_statement(&mut self, stmt: &ReturnStatement, _node: &AstNode) {
1698-
stmt.walk(self)
1699-
}
1700-
1701-
fn visit_jump_statement(&mut self, stmt: &JumpStatement, _node: &AstNode) {
1702-
stmt.walk(self)
1703-
}
1704-
1705-
fn visit_label_statement(&mut self, _stmt: &LabelStatement, _node: &AstNode) {}
1706-
1707-
fn visit_allocation(&mut self, _stmt: &Allocation, _node: &AstNode) {}
1708-
1709-
fn visit_super(&mut self, _stmt: &AstStatement, _node: &AstNode) {
1710-
self.result.push_str("SUPER");
1711-
}
1712-
1713-
fn visit_this(&mut self, _stmt: &AstStatement, _node: &AstNode) {
1714-
self.result.push_str("THIS");
1481+
AstSerializer::format(self)
17151482
}
17161483
}
17171484

@@ -1831,9 +1598,7 @@ impl Operator {
18311598

18321599
#[cfg(test)]
18331600
mod tests {
1834-
use crate::ast::{
1835-
ArgumentProperty, AstFactory, AstNode, DeclarationKind, Operator, PouType, VariableBlockType,
1836-
};
1601+
use crate::ast::{ArgumentProperty, DeclarationKind, PouType, VariableBlockType};
18371602

18381603
#[test]
18391604
fn display_pou() {
@@ -1863,35 +1628,6 @@ mod tests {
18631628
assert_eq!(VariableBlockType::Global.to_string(), "Global");
18641629
assert_eq!(VariableBlockType::InOut.to_string(), "InOut");
18651630
}
1866-
1867-
#[test]
1868-
fn test_as_string() {
1869-
use crate::literals::AstLiteral;
1870-
use plc_source::source_location::SourceLocation;
1871-
1872-
// Test integer literal
1873-
let integer_node = AstNode::new_integer(42, 1, SourceLocation::internal());
1874-
assert_eq!(integer_node.as_string(), "42");
1875-
1876-
// Test identifier
1877-
let identifier_node = AstFactory::create_identifier("myVar", SourceLocation::internal(), 2);
1878-
assert_eq!(identifier_node.as_string(), "myVar");
1879-
1880-
// Test binary expression: 3 + 5
1881-
let left = AstNode::new_integer(3, 3, SourceLocation::internal());
1882-
let right = AstNode::new_integer(5, 4, SourceLocation::internal());
1883-
let binary_expr = AstFactory::create_binary_expression(left, Operator::Plus, right, 5);
1884-
assert_eq!(binary_expr.as_string(), "3 + 5");
1885-
1886-
// Test parenthesized expression: (42)
1887-
let inner = AstNode::new_integer(42, 6, SourceLocation::internal());
1888-
let paren_expr = AstFactory::create_paren_expression(inner, SourceLocation::internal(), 7);
1889-
assert_eq!(paren_expr.as_string(), "(42)");
1890-
1891-
// Test boolean literal
1892-
let bool_node = AstNode::new_literal(AstLiteral::Bool(true), 8, SourceLocation::internal());
1893-
assert_eq!(bool_node.as_string(), "TRUE");
1894-
}
18951631
}
18961632

18971633
pub struct AstFactory {}

0 commit comments

Comments
 (0)