Skip to content

Commit d94bdc7

Browse files
authored
Merge pull request #592 from scratchcpp/llvm_function_static_types
LLVM: Implement function static types
2 parents dc1bcf7 + 78bd34b commit d94bdc7

20 files changed

+1094
-456
lines changed

include/scratchcpp/dev/compiler.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#pragma once
44

55
#include <unordered_set>
6+
#include <vector>
67

78
#include "../global.h"
89
#include "../spimpl.h"
@@ -23,6 +24,14 @@ class CompilerPrivate;
2324
class LIBSCRATCHCPP_EXPORT Compiler
2425
{
2526
public:
27+
enum class StaticType
28+
{
29+
Void,
30+
Number,
31+
Bool,
32+
String
33+
};
34+
2635
Compiler(IEngine *engine, Target *target);
2736
Compiler(const Compiler &) = delete;
2837

@@ -32,7 +41,7 @@ class LIBSCRATCHCPP_EXPORT Compiler
3241

3342
std::shared_ptr<ExecutableCode> compile(std::shared_ptr<Block> startBlock);
3443

35-
void addFunctionCall(const std::string &functionName, int argCount, bool returns);
44+
void addFunctionCall(const std::string &functionName, StaticType returnType = StaticType::Void, const std::vector<StaticType> &argTypes = {});
3645
void addConstValue(const Value &value);
3746
void addVariableValue(Variable *variable);
3847
void addListContents(List *list);

include/scratchcpp/value_functions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ extern "C"
3636
LIBSCRATCHCPP_EXPORT double value_toDouble(const ValueData *v);
3737
LIBSCRATCHCPP_EXPORT bool value_toBool(const ValueData *v);
3838
LIBSCRATCHCPP_EXPORT void value_toString(const ValueData *v, std::string *dst);
39+
LIBSCRATCHCPP_EXPORT char *value_toCString(const ValueData *v);
3940
LIBSCRATCHCPP_EXPORT void value_toUtf16(const ValueData *v, std::u16string *dst);
4041

42+
LIBSCRATCHCPP_EXPORT char *value_doubleToCString(double v);
43+
LIBSCRATCHCPP_EXPORT const char *value_boolToCString(bool v);
44+
LIBSCRATCHCPP_EXPORT double value_stringToDouble(const char *s);
45+
LIBSCRATCHCPP_EXPORT bool value_stringToBool(const char *s);
46+
4147
LIBSCRATCHCPP_EXPORT void value_add(const ValueData *v1, const ValueData *v2, ValueData *dst);
4248
LIBSCRATCHCPP_EXPORT void value_subtract(const ValueData *v1, const ValueData *v2, ValueData *dst);
4349
LIBSCRATCHCPP_EXPORT void value_multiply(const ValueData *v1, const ValueData *v2, ValueData *dst);

include/scratchcpp/valuedata.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ enum class LIBSCRATCHCPP_EXPORT SpecialValue
1818

1919
enum class LIBSCRATCHCPP_EXPORT ValueType
2020
{
21-
Integer = 0,
22-
Double = 1,
23-
Bool = 2,
24-
String = 3,
21+
Number = 0,
22+
Bool = 1,
23+
String = 2,
2524
Infinity = -1,
2625
NegativeInfinity = -2,
2726
NaN = -3
@@ -35,8 +34,7 @@ extern "C"
3534
// NOTE: Any changes must also be done in the LLVM code builder!
3635
union
3736
{
38-
long intValue;
39-
double doubleValue;
37+
double numberValue;
4038
bool boolValue;
4139
char *stringValue;
4240
};

src/dev/engine/compiler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ std::shared_ptr<ExecutableCode> Compiler::compile(std::shared_ptr<Block> startBl
7373
* Adds a call to the given function.\n
7474
* For example: extern "C" some_block(ValueData *ret, ValueData *arg1, ValueData *arg2) has 2 arguments
7575
*/
76-
void Compiler::addFunctionCall(const std::string &functionName, int argCount, bool returns)
76+
void Compiler::addFunctionCall(const std::string &functionName, StaticType returnType, const std::vector<StaticType> &argTypes)
7777
{
78-
impl->builder->addFunctionCall(functionName, argCount, returns);
78+
impl->builder->addFunctionCall(functionName, returnType, argTypes);
7979
}
8080

8181
/*! Adds a constant value to the compiled code. */

src/dev/engine/internal/icodebuilder.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
#pragma once
44

5-
#include <string>
6-
#include <memory>
5+
#include <scratchcpp/dev/compiler.h>
76

87
namespace libscratchcpp
98
{
@@ -20,7 +19,7 @@ class ICodeBuilder
2019

2120
virtual std::shared_ptr<ExecutableCode> finalize() = 0;
2221

23-
virtual void addFunctionCall(const std::string &functionName, int argCount, bool returns) = 0;
22+
virtual void addFunctionCall(const std::string &functionName, Compiler::StaticType returnType, const std::vector<Compiler::StaticType> &argTypes) = 0;
2423
virtual void addConstValue(const Value &value) = 0;
2524
virtual void addVariableValue(Variable *variable) = 0;
2625
virtual void addListContents(List *list) = 0;

0 commit comments

Comments
 (0)