Skip to content
Open
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
2 changes: 2 additions & 0 deletions cpp/core/core_includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ namespace std {
// This module is part of Layer 2B (Host C# Compatibility Layer)
#define CORE_LAYER_2B

#ifdef __GNUC__
// Disable the "unused parameter" warning. Some of our transpiled classes
// (particularly the ASTNode and Parselet hierarchies) make extensive use
// of virtual methods, and in many cases, don't need all the parameters,
// but C# does not allow us to leave such parameters unnamed.
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif /* defined(__GNUC__) */

// Data types which, in C#, are all defined in System.
// C# code should use these instead of the shortcuts (int, byte, long, etc.)
Expand Down
7 changes: 7 additions & 0 deletions cs/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
#ifndef _WIN32
#include <unistd.h>
#endif
#ifdef _WIN32
#include <windows.h>
// POSIX setenv stub
// Contract: we always ignore the return value and always want to overwrite a previous value
// ^^^^^^^^^ if either of these things change in the below code, this will need rewritten
void setenv(const char *name, const char *value, int overwrite) { SetEnvironmentVariableA(name, value); }
#endif
using namespace MiniScript;
*** END CPP_ONLY ***/

Expand Down
2 changes: 1 addition & 1 deletion cs/Assembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ public UInt32 AddLine(String line, Int32 lineNumber) {
Byte reserveRegs = (Byte)ParseInt16(parts[1]); // ToDo: check range before typecast
constantValue = ParseAsConstant(parts[2]);
if (!is_string(constantValue)) {
Error(StringUtils.Format("Function name must be a string"));
Error("Function name must be a string");
return 0;
}
Int32 constIdx = AddConstant(constantValue);
Expand Down
2 changes: 2 additions & 0 deletions cs/GCManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ public static GCHandle GetHandle(Value v) {
return Handles.Get(value_item_index(v));
}

//*** BEGIN CS_ONLY ***
// ── Static helper for content-based string access ─────────────────────────
// Used by GCMap for content-based key hashing and equality.
// (Or is it? ToDo: see if this is still needed.)
Expand All @@ -300,6 +301,7 @@ public static String GetStringContent(Value v) {
}
return "";
}
//*** END CS_ONLY ***
}

}
43 changes: 43 additions & 0 deletions cs/IOHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,49 @@
// CPP: #include <string>
// CPP: #include <algorithm>

/*** BEGIN CPP_ONLY ***
#ifdef _WIN32 // define POSIX getline if on Windows
#include <cstdio>
#include <cstdlib>

int getline(char** lineptr, size_t* n, FILE* stream) {
if (!lineptr || !n || !stream) return -1;

size_t pos = 0;

// Allocate if needed
if (*lineptr == nullptr || *n == 0) {
*n = 128;
*lineptr = (char*)malloc(*n);
if (!*lineptr) return -1;
}

int c;
while ((c = fgetc(stream)) != EOF) {
// Grow buffer if needed
if (pos + 1 >= *n) {
size_t new_size = *n * 2;
char* new_ptr = (char*)realloc(*lineptr, new_size);
if (!new_ptr) return -1;
*lineptr = new_ptr;
*n = new_size;
}

(*lineptr)[pos++] = (char)c;

if (c == '\n') break;
}

if (pos == 0 && c == EOF) {
return -1; // EOF with no data
}

(*lineptr)[pos] = '\0';
return (int)pos;
}
#endif // defined(_WIN32)
*** END CPP_ONLY ***/

namespace MiniScript {

public enum TextStyle : Int32 {
Expand Down
2 changes: 1 addition & 1 deletion cs/IntrinsicAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Value GetVar(String variableName) {
return vm.LookupParamByName(variableName);
}

public Interpreter Interpreter() {
public Interpreter GetInterpreter() {
return vm.GetInterpreter();
}
}
Expand Down
8 changes: 4 additions & 4 deletions cs/ShellIntrinsics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@
#include <sys/sendfile.h>
#endif
#define PATHSEP "/"
extern "C" {
extern char **environ;
}
#endif
#include <cerrno>
#include <ctime>
extern "C" {
extern char **environ;
}
*** END CPP_ONLY ***/

namespace MiniScript {
Expand Down Expand Up @@ -1088,7 +1088,7 @@ struct tm t;
localtime_r(&stats.st_mtime, &t);
#endif
#endif
char dateBuf[32];
char dateBuf[72];
snprintf(dateBuf, sizeof(dateBuf), "%04d-%02d-%02d %02d:%02d:%02d",
1900 + t.tm_year, 1 + t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
Value result = make_map(4);
Expand Down
1 change: 1 addition & 0 deletions cs/VM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// H: #include "ErrorTypes.g.h"
// H: #include "value_map.h"
// H: #include <vector>
// H: #include <chrono>
// H: #include "GCManager.g.h"
// CPP: #include "value_list.h"
// CPP: #include "value_string.h"
Expand Down
11 changes: 9 additions & 2 deletions cs/VMVis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,15 @@ public void UpdateScreenSize() {
//*** END CS_ONLY ***
/*** BEGIN CPP_ONLY ***
#ifdef _WIN32
_screenWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1;
_screenHeight = rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
HANDLE hStdout;
CONSOLE_SCREEN_BUFFER_INFO csbi;
if ((hStdout = GetStdHandle(STD_OUTPUT_HANDLE))!=INVALID_HANDLE_VALUE && GetConsoleScreenBufferInfo(hStdout, &csbi)) {
_screenWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1;
_screenHeight = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
} else {
_screenWidth = 80;
_screenHeight = 24;
}
#else
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
Expand Down
7 changes: 7 additions & 0 deletions generated/App.g.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
#ifndef _WIN32
#include <unistd.h>
#endif
#ifdef _WIN32
#include <windows.h>
// POSIX setenv stub
// Contract: we always ignore the return value and always want to overwrite a previous value
// ^^^^^^^^^ if either of these things change in the below code, this will need rewritten
void setenv(const char *name, const char *value, int overwrite) { SetEnvironmentVariableA(name, value); }
#endif
using namespace MiniScript;

int main(int argc, const char* argv[]) {
Expand Down
3 changes: 2 additions & 1 deletion generated/Assembler.g.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ UInt32 AssemblerStorage::AddLine(String line,Int32 lineNumber) {
Byte reserveRegs = (Byte)ParseInt16(parts[1]); // ToDo: check range before typecast
constantValue = ParseAsConstant(parts[2]);
if (!is_string(constantValue)) {
Error(StringUtils::Format("Function name must be a string"));
Error("Function name must be a string");
return 0;
}
Int32 constIdx = AddConstant(constantValue);
Expand Down Expand Up @@ -771,6 +771,7 @@ String AssemblerStorage::ParseLabel(String token) {
return token.Substring(0, token.Length()-1);
}
bool AssemblerStorage::AddFunction(String functionName) {

if (HasFunction(functionName)) {
IOHelper::Print(StringUtils::Format("ERROR: Function {0} is defined multiple times", functionName));
return Boolean(false);
Expand Down
1 change: 1 addition & 0 deletions generated/Bytecode.g.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ EmitPattern BytecodeUtil::GetEmitPattern(Opcode opcode) {
// _rA_rB_rC, _rA_rB_iC, _rA_iB_rC, _iA_rB_iC, _rA_rB_kC, _rA_rB (two registers)
if (mnemonic.Contains("_rA_rB_rC") || mnemonic.Contains("_rA_rB_iC") ||
mnemonic.Contains("_rA_iB_rC") || mnemonic.Contains("_iA_rB_iC") ||

mnemonic.Contains("_rA_rB_kC") || mnemonic.EndsWith("_rA_rB")) {
return EmitPattern::ABC;
}
Expand Down
2 changes: 2 additions & 0 deletions generated/CodeGenerator.g.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ CodeGeneratorStorage::CodeGeneratorStorage(CodeEmitterBase emitter) {
}
List<FuncDef> CodeGeneratorStorage::GetFunctions() {
return _functions;

}
Int32 CodeGeneratorStorage::AllocReg() {
// Scan from _firstAvailable to find first free register
Expand Down Expand Up @@ -407,6 +408,7 @@ Int32 CodeGeneratorStorage::Visit(BinaryOpNode node) {
}

Int32 resultReg = GetTargetOrAlloc(); // Capture target before any recursive calls

Int32 leftReg = node.Left().Accept(_this);
Int32 rightReg = node.Right().Accept(_this);

Expand Down
1 change: 1 addition & 0 deletions generated/GCItems.g.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Int32 GCList::IndexOf(Value item,Int32 afterIdx) {
void GCList::MarkChildren() {
if (IsNull(Items)) return;
for (Int32 i = 0; i < Items.Count(); i++) GCManager::Mark(Items[i]);

}

Int32 GCMap::Count() {
Expand Down
20 changes: 0 additions & 20 deletions generated/GCManager.g.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ class GCManager {
public: static GCFunction GetFuncRef(Value v);
public: static GCHandle GetHandle(Value v);

// ── Static helper for content-based string access ─────────────────────────
// Used by GCMap for content-based key hashing and equality.
// (Or is it? ToDo: see if this is still needed.)

public: static String GetStringContent(Value v);
}; // end of struct GCManager

// INLINE METHODS
Expand All @@ -133,20 +128,5 @@ inline void GCManager::Mark(Value v) {
if (!is_gc_object(v)) return;
DispatchMark(value_gc_set_index(v), value_item_index(v));
}
inline String GCManager::GetStringContent(Value v) {
if (is_tiny_string(v)) {
Int32 len = value_tiny_len(v);
char chars[len];
for (Int32 i = 0; i < len; i++) chars[i] = (char)((value_bits(v) >> (8 * (i + 1))) & 0xFF);
return String::New(chars);
}
if (is_heap_string(v)) {
GCStringSet set;
set = (value_gc_set_index(v) == InternedStringSet) ? InternedStrings : BigStrings;
String data = set.Get(value_item_index(v)).Data;
return !IsNull(data) ? data : "";
}
return "";
}

} // end of namespace MiniScript
2 changes: 2 additions & 0 deletions generated/GCSet.g.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class GCErrorSetStorage : public GCSetBaseStorage {
public: GCErrorSetStorage(Int32 initialCapacity = 64);

protected: void CallMarkChildren(Int32 idx);

protected: void CallOnSweep(Int32 idx);
protected: void AppendItem();

Expand Down Expand Up @@ -309,6 +310,7 @@ struct GCErrorSet : public GCSetBase {
}

protected: void CallMarkChildren(Int32 idx) { return get()->CallMarkChildren(idx); }

protected: void CallOnSweep(Int32 idx) { return get()->CallOnSweep(idx); }
protected: void AppendItem() { return get()->AppendItem(); }

Expand Down
40 changes: 40 additions & 0 deletions generated/IOHelper.g.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,46 @@
#include <fstream>
#include <string>
#include <algorithm>
#ifdef _WIN32 // define POSIX getline if on Windows
#include <cstdio>
#include <cstdlib>

int getline(char** lineptr, size_t* n, FILE* stream) {
if (!lineptr || !n || !stream) return -1;

size_t pos = 0;

// Allocate if needed
if (*lineptr == nullptr || *n == 0) {
*n = 128;
*lineptr = (char*)malloc(*n);
if (!*lineptr) return -1;
}

int c;
while ((c = fgetc(stream)) != EOF) {
// Grow buffer if needed
if (pos + 1 >= *n) {
size_t new_size = *n * 2;
char* new_ptr = (char*)realloc(*lineptr, new_size);
if (!new_ptr) return -1;
*lineptr = new_ptr;
*n = new_size;
}

(*lineptr)[pos++] = (char)c;

if (c == '\n') break;
}

if (pos == 0 && c == EOF) {
return -1; // EOF with no data
}

(*lineptr)[pos] = '\0';
return (int)pos;
}
#endif // defined(_WIN32)

namespace MiniScript {

Expand Down
1 change: 1 addition & 0 deletions generated/Interpreter.g.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ void InterpreterStorage::REPL(String sourceLine,double timeLimit) {
Error = val_null;
if (IsNull(parser)) parser = Parser::New();
parser.Init(_pendingSource);

List<ASTNode> statements = parser.ParseProgram();

// If parser needs more input, return and wait for next line
Expand Down
2 changes: 2 additions & 0 deletions generated/Interpreter.g.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class InterpreterStorage : public std::enable_shared_from_this<InterpreterStorag
private: Value _replGlobals = val_null; // persistent globals VarMap

//

// standardOutput: receives the output of the "print" intrinsic.
//

Expand Down Expand Up @@ -272,6 +273,7 @@ struct Interpreter {
public: Interpreter(InterpreterStorage* p) : storage(p ? p->shared_from_this() : nullptr) {}

//

// standardOutput: receives the output of the "print" intrinsic.
//

Expand Down
2 changes: 1 addition & 1 deletion generated/IntrinsicAPI.g.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace MiniScript {
Value Context::GetVar(String variableName) {
return vm.LookupParamByName(variableName);
}
Interpreter Context::Interpreter() {
Interpreter Context::GetInterpreter() {
return vm.GetInterpreter();
}

Expand Down
2 changes: 1 addition & 1 deletion generated/IntrinsicAPI.g.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct Context {
// argument values, which are far more efficiently found via GetArg (above).
public: Value GetVar(String variableName);

public: Interpreter Interpreter();
public: Interpreter GetInterpreter();
}; // end of struct Context

// IntrinsicResult: represents the result of calling an intrinsic function
Expand Down
1 change: 1 addition & 0 deletions generated/LangConstants.g.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ enum class TokenType : Int32 {
COLON,
DOT,
NOT,

AND,
OR,
WHILE,
Expand Down
1 change: 1 addition & 0 deletions generated/Parser.g.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ Precedence ParserStorage::GetPrecedence() {
InfixParselet parselet = nullptr;
if (_infixParselets.TryGetValue(_current.Type, &parselet)) {
return parselet.Prec();

}
return Precedence::NONE;
}
Expand Down
Loading