Skip to content

Commit 37f60f2

Browse files
committed
Add basic Statement List parser
Includes support for simple arithmetic and boolean expressions as well as other Statement List language features, such as networks, functions and function blocks.
1 parent 3f9e1b0 commit 37f60f2

18 files changed

+1954
-0
lines changed

Diff for: .gitignore

+4
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/statement-list/statement_list_lex.yy.cpp
88+
src/statement-list/statement_list_y.output
89+
src/statement-list/statement_list_y.tab.cpp
90+
src/statement-list/statement_list_y.tab.h
8791
src/xmllang/xml_lex.yy.cpp
8892
src/xmllang/xml_y.output
8993
src/xmllang/xml_y.tab.cpp
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*******************************************************************\
2+
3+
Module: Statement List Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Statement List Language Conversion
11+
12+
#include "convert_bool_literal.h"
13+
14+
#include <algorithm>
15+
#include <util/std_types.h>
16+
// Needed for back_inserter in Visual Studio.
17+
#include <iterator>
18+
19+
constant_exprt convert_bool_literal(const std::string &src)
20+
{
21+
std::string copy;
22+
transform(begin(src), end(src), back_inserter(copy), ::tolower);
23+
24+
bool_typet type;
25+
type.set(ID_statement_list_type, ID_statement_list_bool);
26+
27+
return constant_exprt(copy, type);
28+
}

Diff for: src/statement-list/converters/convert_bool_literal.h

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*******************************************************************\
2+
3+
Module: Statement List Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Statement List Language Conversion
11+
12+
#ifndef CPROVER_STATEMENT_LIST_CONVERTERS_CONVERT_BOOL_LITERAL_H
13+
#define CPROVER_STATEMENT_LIST_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_STATEMENT_LIST_CONVERTERS_CONVERT_INT_LITERAL_H
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*******************************************************************\
2+
3+
Module: Statement List Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Statement List Language Conversion
11+
12+
#include "convert_int_literal.h"
13+
14+
#include <algorithm>
15+
#include <util/std_types.h>
16+
17+
/// Hex literals in STL are preceded by '16#'
18+
#define HEX_PREFIX_LENGTH 3
19+
/// Binary literals in STL are preceded by '2#'
20+
#define BINARY_PREFIX_LENGTH 2
21+
22+
constant_exprt convert_int_dec_literal(const std::string &src)
23+
{
24+
std::string value(src);
25+
value.erase(std::remove(begin(value), end(value), '+'), end(value));
26+
27+
signedbv_typet type{16};
28+
type.set(ID_statement_list_type, ID_statement_list_int);
29+
30+
return constant_exprt(value, type);
31+
}
32+
33+
constant_exprt convert_int_hex_literal(const std::string &src)
34+
{
35+
const std::string hex_literal(
36+
src.substr(HEX_PREFIX_LENGTH, std::string::npos));
37+
int value = std::stoi(hex_literal, nullptr, 16);
38+
39+
signedbv_typet type{16};
40+
type.set(ID_statement_list_type, ID_statement_list_int);
41+
42+
return constant_exprt(std::to_string(value), type);
43+
}
44+
45+
constant_exprt convert_int_bit_literal(const std::string &src)
46+
{
47+
const std::string bit_literal(
48+
src.substr(BINARY_PREFIX_LENGTH, std::string::npos));
49+
int value = std::stoi(bit_literal, nullptr, 2);
50+
51+
signedbv_typet type{16};
52+
type.set(ID_statement_list_type, ID_statement_list_int);
53+
54+
return constant_exprt{std::to_string(value), type};
55+
}

Diff for: src/statement-list/converters/convert_int_literal.h

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*******************************************************************\
2+
3+
Module: Statement List Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Statement List Language Conversion
11+
12+
#ifndef CPROVER_STATEMENT_LIST_CONVERTERS_CONVERT_INT_LITERAL_H
13+
#define CPROVER_STATEMENT_LIST_CONVERTERS_CONVERT_INT_LITERAL_H
14+
15+
#include <util/expr.h>
16+
#include <util/std_expr.h>
17+
18+
/// Converts a string into the corresponding 'Int' expression.
19+
/// \param src: String returned by the parser (base 10).
20+
/// \return Constant expression representing the integer value.
21+
constant_exprt convert_int_dec_literal(const std::string &src);
22+
23+
/// Converts a string into the corresponding 'Int' expression.
24+
/// \param src: String returned by the parser (base 16).
25+
/// \return Constant expression representing the integer value.
26+
constant_exprt convert_int_hex_literal(const std::string &src);
27+
28+
/// Converts a string into the corresponding 'Int' expression.
29+
/// \param src: String returned by the parser (base 2).
30+
/// \return Constant expression representing the integer value.
31+
constant_exprt convert_int_bit_literal(const std::string &src);
32+
33+
#endif // CPROVER_STATEMENT_LIST_CONVERTERS_CONVERT_INT_LITERAL_H
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*******************************************************************\
2+
3+
Module: Statement List Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Statement List Language Conversion
11+
12+
#include "convert_real_literal.h"
13+
14+
#include <util/ieee_float.h>
15+
#include <util/std_expr.h>
16+
17+
constant_exprt convert_real_literal(const std::string &src)
18+
{
19+
floatbv_typet type = ieee_float_spect::single_precision().to_type();
20+
type.set(ID_statement_list_type, ID_statement_list_real);
21+
22+
return constant_exprt(src, type);
23+
}

Diff for: src/statement-list/converters/convert_real_literal.h

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*******************************************************************\
2+
3+
Module: Statement List Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Statement List Language Conversion
11+
12+
#ifndef CPROVER_STATEMENT_LIST_CONVERTERS_CONVERT_REAL_LITERAL_H
13+
#define CPROVER_STATEMENT_LIST_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_STATEMENT_LIST_CONVERTERS_CONVERT_REAL_VALUE_H
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*******************************************************************\
2+
3+
Module: Statement List Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Statement List 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_statement_list_type, ID_statement_list_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_statement_list_type, ID_statement_list_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+
}

Diff for: src/statement-list/converters/convert_string_value.h

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*******************************************************************\
2+
3+
Module: Statement List Language Conversion
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Statement List Language Conversion
11+
12+
#ifndef CPROVER_STATEMENT_LIST_CONVERTERS_CONVERT_STRING_VALUE_H
13+
#define CPROVER_STATEMENT_LIST_CONVERTERS_CONVERT_STRING_VALUE_H
14+
15+
#include <util/std_code.h>
16+
#include <util/string_constant.h>
17+
18+
/// Converts a string into a Statement List identifier.
19+
/// \param src: String returned by the parser.
20+
/// \return Constant string expression representing the identifier.
21+
string_constantt convert_identifier(const std::string &src);
22+
23+
/// Converts a string into a Statement List title.
24+
/// \param src: String returned by the parser.
25+
/// \return Constant string expression representing the title.
26+
string_constantt convert_title(const std::string &src);
27+
28+
/// Converts a string into a Statement List label.
29+
/// \param src: String returned by the parser.
30+
/// \return Code label expression representing the label.
31+
code_labelt convert_label(const std::string &src);
32+
33+
#endif // CPROVER_STATEMENT_LIST_CONVERTERS_CONVERT_STRING_VALUE_H
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
statement-list
2+
statement-list/converters
3+
util

Diff for: src/statement-list/module_dependencies.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
converters
2+
langapi
3+
linking
4+
statement-list
5+
util

0 commit comments

Comments
 (0)