Skip to content

Commit 3a4dfcf

Browse files
committed
Add basic Structured Text parser
Includes support for simple arithmetic and boolean expressions as well as other ST language features, such as networks, functions and function blocks.
1 parent 7734b53 commit 3a4dfcf

18 files changed

+1954
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ src/json/json_lex.yy.cpp
8484
src/json/json_y.output
8585
src/json/json_y.tab.cpp
8686
src/json/json_y.tab.h
87+
src/structured-text/structured_text_lex.yy.cpp
88+
src/structured-text/structured_text_y.output
89+
src/structured-text/structured_text_y.tab.cpp
90+
src/structured-text/structured_text_y.tab.h
8791
src/xmllang/xml_lex.yy.cpp
8892
src/xmllang/xml_y.output
8993
src/xmllang/xml_y.tab.cpp
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*******************************************************************\
2+
3+
Module: Structured Text Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Structured Text Language Conversion
11+
12+
#include "convert_bool_literal.h"
13+
#include <algorithm>
14+
#include <util/std_types.h>
15+
16+
constant_exprt convert_bool_literal(const std::string &src)
17+
{
18+
// Transform to lower case.
19+
std::string copy;
20+
std::transform(src.begin(), src.end(), copy.begin(), ::tolower);
21+
22+
bool_typet type;
23+
type.set(ID_structured_text_type, ID_structured_text_bool);
24+
25+
return constant_exprt(copy, type);
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*******************************************************************\
2+
3+
Module: Structured Text Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Structured Text Language Conversion
11+
12+
#ifndef CPROVER_STRUCTURED_TEXT_CONVERTERS_CONVERT_BOOL_LITERAL_H
13+
#define CPROVER_STRUCTURED_TEXT_CONVERTERS_CONVERT_BOOL_LITERAL_H
14+
15+
#include <string>
16+
17+
#include <util/expr.h>
18+
#include <util/std_expr.h>
19+
20+
/// Converts a string into the corresponding 'Bool' expression.
21+
/// \param src: String returned by the parser.
22+
/// \return Constant expression representing the boolean value.
23+
constant_exprt convert_bool_literal(const std::string &src);
24+
25+
#endif // CPROVER_STRUCTURED_TEXT_CONVERTERS_CONVERT_INT_LITERAL_H
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*******************************************************************\
2+
3+
Module: Structured Text Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Structured Text Language Conversion
11+
12+
#include "convert_int_literal.h"
13+
#include <algorithm>
14+
#include <util/std_types.h>
15+
16+
constant_exprt convert_int_dec_literal(const std::string &src)
17+
{
18+
// Remove '+' char if present.
19+
std::string value(src);
20+
value.erase(std::remove(value.begin(), value.end(), '+'), value.end());
21+
22+
signedbv_typet type{16};
23+
type.set(ID_structured_text_type, ID_structured_text_int);
24+
25+
return constant_exprt(value, type);
26+
}
27+
28+
constant_exprt convert_int_hex_literal(const std::string &src)
29+
{
30+
// Cut the leading '16#' and convert to int
31+
std::string cut = src.substr(3, std::string::npos);
32+
int value = std::stoi(cut, nullptr, 16);
33+
34+
signedbv_typet type{16};
35+
type.set(ID_structured_text_type, ID_structured_text_int);
36+
37+
return constant_exprt(std::to_string(value), type);
38+
}
39+
40+
constant_exprt convert_int_bit_literal(const std::string &src)
41+
{
42+
// Cut the leading '2#' and convert to int
43+
std::string cut = src.substr(2, std::string::npos);
44+
int value = std::stoi(cut, nullptr, 2);
45+
46+
signedbv_typet type{16};
47+
type.set(ID_structured_text_type, ID_structured_text_int);
48+
49+
return constant_exprt(std::to_string(value), type);
50+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*******************************************************************\
2+
3+
Module: Structured Text Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Structured Text Language Conversion
11+
12+
#ifndef CPROVER_STRUCTURED_TEXT_CONVERTERS_CONVERT_INT_LITERAL_H
13+
#define CPROVER_STRUCTURED_TEXT_CONVERTERS_CONVERT_INT_LITERAL_H
14+
15+
#include <string>
16+
17+
#include <util/expr.h>
18+
#include <util/std_expr.h>
19+
20+
/// Converts a string into the corresponding 'Int' expression.
21+
/// \param src: String returned by the parser (base 10).
22+
/// \return Constant expression representing the integer value.
23+
constant_exprt convert_int_dec_literal(const std::string &src);
24+
25+
/// Converts a string into the corresponding 'Int' expression.
26+
/// \param src: String returned by the parser (base 16).
27+
/// \return Constant expression representing the integer value.
28+
constant_exprt convert_int_hex_literal(const std::string &src);
29+
30+
/// Converts a string into the corresponding 'Int' expression.
31+
/// \param src: String returned by the parser (base 2).
32+
/// \return Constant expression representing the integer value.
33+
constant_exprt convert_int_bit_literal(const std::string &src);
34+
35+
#endif // CPROVER_STRUCTURED_TEXT_CONVERTERS_CONVERT_INT_LITERAL_H
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*******************************************************************\
2+
3+
Module: Structured Text Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Structured Text Language Conversion
11+
12+
#include "convert_real_literal.h"
13+
#include <util/ieee_float.h>
14+
#include <util/std_expr.h>
15+
16+
constant_exprt convert_real_literal(const std::string &src)
17+
{
18+
floatbv_typet type = ieee_float_spect::single_precision().to_type();
19+
type.set(ID_structured_text_type, ID_structured_text_real);
20+
21+
return constant_exprt(src, type);
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*******************************************************************\
2+
3+
Module: Structured Text Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Structured Text Language Conversion
11+
12+
#ifndef CPROVER_STRUCTURED_TEXT_CONVERTERS_CONVERT_REAL_LITERAL_H
13+
#define CPROVER_STRUCTURED_TEXT_CONVERTERS_CONVERT_REAL_LITERAL_H
14+
15+
#include <string>
16+
17+
#include <util/expr.h>
18+
#include <util/std_types.h>
19+
20+
/// Converts a string into the corresponding 'Real' expression.
21+
/// \param src: String returned by the parser.
22+
/// \return Constant expression representing the real value.
23+
constant_exprt convert_real_literal(const std::string &src);
24+
25+
#endif // CPROVER_STRUCTURED_TEXT_CONVERTERS_CONVERT_REAL_VALUE_H
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*******************************************************************\
2+
3+
Module: Structured Text Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Structured Text Language Conversion
11+
12+
#include "convert_string_value.h"
13+
14+
string_constantt convert_identifier(const std::string &src)
15+
{
16+
string_constantt result{src};
17+
result.set(ID_structured_text_type, ID_structured_text_identifier);
18+
return result;
19+
}
20+
21+
string_constantt convert_title(const std::string &src)
22+
{
23+
string_constantt result{src};
24+
result.set(ID_structured_text_type, ID_structured_text_title);
25+
return result;
26+
}
27+
28+
code_labelt convert_label(const std::string &src)
29+
{
30+
// Cut the trailing colon
31+
std::string value = src.substr(0, src.length() - 1);
32+
33+
return code_labelt{value, codet(ID_label)};
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*******************************************************************\
2+
3+
Module: Structured Text Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Structured Text Language Conversion
11+
12+
#ifndef CPROVER_STRUCTURED_TEXT_CONVERTERS_CONVERT_STRING_VALUE_H
13+
#define CPROVER_STRUCTURED_TEXT_CONVERTERS_CONVERT_STRING_VALUE_H
14+
15+
#include <util/expr.h>
16+
#include <util/std_code.h>
17+
#include <util/string_constant.h>
18+
19+
/// Converts a string into a Structured Text identifier.
20+
/// \param src: String returned by the parser.
21+
/// \return Constant string expression representing the identifier.
22+
string_constantt convert_identifier(const std::string &src);
23+
24+
/// Converts a string into a Structured Text title.
25+
/// \param src: String returned by the parser.
26+
/// \return Constant string expression representing the title.
27+
string_constantt convert_title(const std::string &src);
28+
29+
/// Converts a string into a Structured Text label.
30+
/// \param src: String returned by the parser.
31+
/// \return Code label expression representing the label.
32+
code_labelt convert_label(const std::string &src);
33+
34+
#endif // CPROVER_STRUCTURED_TEXT_CONVERTERS_CONVERT_STRING_VALUE_H
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
structured-text
2+
structured-text/converters
3+
util
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
converters
2+
langapi
3+
linking
4+
structured-text
5+
util

0 commit comments

Comments
 (0)