Skip to content

Commit

Permalink
ext/MiniScript: updated MiniScript
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasdr committed Jan 17, 2024
1 parent e9e34ec commit 39d25ed
Show file tree
Hide file tree
Showing 25 changed files with 587 additions and 40 deletions.
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
MIT License

Developed 2012-2023 by Andreas Drewke, Dominik Hepp, Mathias Lenz, Kolja Gumpert,
Developed 2012-2024 by Andreas Drewke, Dominik Hepp, Mathias Lenz, Kolja Gumpert,
drewke.net, mindty.com, github.com/mahula

Copyright 2012...2023 Andreas Drewke and others if not stated differently in
Copyright 2012...2024 Andreas Drewke and others if not stated differently in
header file.

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,7 @@ EXT_MINISCRIPT_SRCS = \
ext/miniscript/src/miniscript/os/network/TCPSocket.cpp \
ext/miniscript/src/miniscript/utilities/Base64.cpp \
ext/miniscript/src/miniscript/utilities/Console.cpp \
ext/miniscript/src/miniscript/utilities/ErrorConsole.cpp \
ext/miniscript/src/miniscript/utilities/ExceptionBase.cpp \
ext/miniscript/src/miniscript/utilities/Float.cpp \
ext/miniscript/src/miniscript/utilities/Hex.cpp \
Expand Down
1 change: 1 addition & 0 deletions Makefile.nmake
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,7 @@ EXT_MINISCRIPT_SRCS = \
ext/miniscript/src/miniscript/os/network/TCPSocket.cpp \
ext/miniscript/src/miniscript/utilities/Base64.cpp \
ext/miniscript/src/miniscript/utilities/Console.cpp \
ext/miniscript/src/miniscript/utilities/ErrorConsole.cpp \
ext/miniscript/src/miniscript/utilities/ExceptionBase.cpp \
ext/miniscript/src/miniscript/utilities/Float.cpp \
ext/miniscript/src/miniscript/utilities/Hex.cpp \
Expand Down
37 changes: 26 additions & 11 deletions README-MiniScript.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions ext/miniscript/LICENSE
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
MIT License

Developed 2021-2023 by Andreas Drewke, Dominik Hepp, Kolja Gumpert,
Developed 2021-2024 by Andreas Drewke, Dominik Hepp, Kolja Gumpert,
drewke.net, mindty.com

Copyright 2023 Andreas Drewke.
Copyright 2024 Andreas Drewke.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
68 changes: 68 additions & 0 deletions ext/miniscript/src/miniscript/miniscript/ApplicationMethods.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <array>
#include <cstdio>
#include <cstdlib>
#include <span>
#include <memory>

Expand All @@ -21,6 +22,43 @@ using _Console = miniscript::utilities::Console;
using _StringTools = miniscript::utilities::StringTools;

void ApplicationMethods::registerConstants(MiniScript* miniScript) {
miniScript->setConstant("$APPLICATION::EXITCODE_SUCCESS", static_cast<int64_t>(EXIT_SUCCESS));
miniScript->setConstant("$APPLICATION::EXITCODE_FAILURE", static_cast<int64_t>(EXIT_FAILURE));
//
#if defined(__FreeBSD__)
miniScript->setConstant("$APPLICATION::OS", string("FreeBSD"));
#elif defined(__HAIKU__)
miniScript->setConstant("$APPLICATION::OS", string("Haiku"));
#elif defined(__linux__)
miniScript->setConstant("$APPLICATION::OS", string("Linux"));
#elif defined(__APPLE__)
miniScript->setConstant("$APPLICATION::OS", string("MacOSX"));
#elif defined(__NetBSD__)
miniScript->setConstant("$APPLICATION::OS", string("NetBSD"));
#elif defined(__OpenBSD__)
miniScript->setConstant("$APPLICATION::OS", string("OpenBSD"));
#elif defined(_MSC_VER)
miniScript->setConstant("$APPLICATION::OS", string("Windows-MSC"));
#elif defined(_WIN32)
miniScript->setConstant("$APPLICATION::OS", string("Windows-MINGW"));
#else
miniScript->setConstant("$APPLICATION::OS", string("Unknown"));
#endif
#if defined(__amd64__) || defined(_M_X64)
miniScript->setConstant("$APPLICATION::CPU", string("X64"));
#elif defined(__ia64__) || defined(_M_IA64)
miniScript->setConstant("$APPLICATION::CPU", string("IA64"));
#elif defined(__aarch64__)
miniScript->setConstant("$APPLICATION::CPU", string("ARM64"));
#elif defined(__arm__) || defined(_M_ARM)
miniScript->setConstant("$APPLICATION::CPU", string("ARM"));
#elif defined(__powerpc64__)
miniScript->setConstant("$APPLICATION::CPU", string("PPC64"));
#elif defined(__powerpc__)
miniScript->setConstant("$APPLICATION::CPU", string("PPC"));
#else
miniScript->setConstant("$APPLICATION::CPU", string("Unknown"));
#endif
}

const string ApplicationMethods::execute(const string& command) {
Expand Down Expand Up @@ -95,5 +133,35 @@ void ApplicationMethods::registerMethods(MiniScript* miniScript) {
};
miniScript->registerMethod(new MethodApplicationGetArguments(miniScript));
}
{
//
class MethodApplicationExit: public MiniScript::Method {
private:
MiniScript* miniScript { nullptr };
public:
MethodApplicationExit(MiniScript* miniScript):
MiniScript::Method(
{
{ .type = MiniScript::TYPE_INTEGER, .name = "exitCode", .optional = true, .reference = false, .nullable = false },
}
),
miniScript(miniScript) {}
const string getMethodName() override {
return "application.exit";
}
void executeMethod(span<MiniScript::Variable>& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) override {
int64_t exitCode = 0ll;
if (MiniScript::getIntegerValue(arguments, 0, exitCode, true) == true) {
miniScript->getContext()->setExitCode(static_cast<int>(exitCode));
miniScript->stopScriptExecution();
miniScript->stopRunning();
} else {
_Console::println(getMethodName() + "(): " + miniScript->getStatementInformation(statement) + ": argument mismatch: expected arguments: " + miniScript->getArgumentInformation(getMethodName()));
miniScript->startErrorScript();
}
}
};
miniScript->registerMethod(new MethodApplicationExit(miniScript));
}
}
}
111 changes: 111 additions & 0 deletions ext/miniscript/src/miniscript/miniscript/ConsoleMethods.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
#include <iostream>
#include <span>

#include <miniscript/miniscript.h>
#include <miniscript/miniscript/ConsoleMethods.h>
#include <miniscript/miniscript/MiniScript.h>
#include <miniscript/utilities/Console.h>
#include <miniscript/utilities/ErrorConsole.h>

using std::cin;
using std::getline;
using std::span;

using miniscript::miniscript::ConsoleMethods;

using miniscript::miniscript::MiniScript;

using _Console = miniscript::utilities::Console;
using _ErrorConsole = miniscript::utilities::ErrorConsole;

void ConsoleMethods::registerConstants(MiniScript* miniScript) {
}
Expand Down Expand Up @@ -110,4 +115,110 @@ void ConsoleMethods::registerMethods(MiniScript* miniScript) {
};
miniScript->registerMethod(new MethodConsolePrintln(miniScript));
}
{
//
class MethodConsoleReadln: public MiniScript::Method {
private:
MiniScript* miniScript { nullptr };
public:
MethodConsoleReadln(MiniScript* miniScript):
MiniScript::Method({}, MiniScript::TYPE_STRING),
miniScript(miniScript) {
//
}
const string getMethodName() override {
return "console.readln";
}
void executeMethod(span<MiniScript::Variable>& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) override {
returnValue.setValue(_Console::readln());
}
};
miniScript->registerMethod(new MethodConsoleReadln(miniScript));
}
{
//
class MethodConsoleReadAll: public MiniScript::Method {
private:
MiniScript* miniScript { nullptr };
public:
MethodConsoleReadAll(MiniScript* miniScript):
MiniScript::Method({}, MiniScript::TYPE_STRING),
miniScript(miniScript) {
//
}
const string getMethodName() override {
return "console.readAll";
}
void executeMethod(span<MiniScript::Variable>& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) override {
returnValue.setValue(_Console::readAll());
}
};
miniScript->registerMethod(new MethodConsoleReadAll(miniScript));
}
{
//
class MethodConsoleReadAllAsArray: public MiniScript::Method {
private:
MiniScript* miniScript { nullptr };
public:
MethodConsoleReadAllAsArray(MiniScript* miniScript):
MiniScript::Method({}, MiniScript::TYPE_ARRAY),
miniScript(miniScript) {
//
}
const string getMethodName() override {
return "console.readAllAsArray";
}
void executeMethod(span<MiniScript::Variable>& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) override {
returnValue.setType(MiniScript::TYPE_ARRAY);
const auto input = _Console::readAllAsArray();
for (const auto& line: input) returnValue.pushArrayEntry(MiniScript::Variable(line));
}
};
miniScript->registerMethod(new MethodConsoleReadAllAsArray(miniScript));
}
{
//
class MethodConsoleErrorPrint: public MiniScript::Method {
private:
MiniScript* miniScript { nullptr };
public:
MethodConsoleErrorPrint(MiniScript* miniScript): MiniScript::Method(), miniScript(miniScript) {}
const string getMethodName() override {
return "console.error.print";
}
void executeMethod(span<MiniScript::Variable>& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) override {
for (const auto& argument: arguments) {
_ErrorConsole::print(argument.getValueAsString());
}
}
bool isVariadic() const override {
return true;
}
};
miniScript->registerMethod(new MethodConsoleErrorPrint(miniScript));
}
{
//
class MethodConsoleErrorPrintln: public MiniScript::Method {
private:
MiniScript* miniScript { nullptr };
public:
MethodConsoleErrorPrintln(MiniScript* miniScript): MiniScript::Method(), miniScript(miniScript) {}
const string getMethodName() override {
return "console.error.println";
}
void executeMethod(span<MiniScript::Variable>& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) override {
for (const auto& argument: arguments) {
_ErrorConsole::print(argument.getValueAsString());
}
_ErrorConsole::println();
}
bool isVariadic() const override {
return true;
}
};
miniScript->registerMethod(new MethodConsoleErrorPrintln(miniScript));
}
}

16 changes: 16 additions & 0 deletions ext/miniscript/src/miniscript/miniscript/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class miniscript::miniscript::Context
unordered_map<string, MiniScript*> scriptsById;
vector<MiniScript*> scriptCallStack;
vector<string> argumentValues;
int exitCode { 0 };

public:
// forbid class copy
Expand Down Expand Up @@ -103,4 +104,19 @@ class miniscript::miniscript::Context
scriptCallStack.erase(scriptCallStack.begin() + scriptCallStack.size() - 1);
}

/**
* Set exit code
* @param exitCode exit code
*/
inline void setExitCode(int exitCode) {
this->exitCode = exitCode;
}

/**
* @return exit code
*/
inline int getExitCode() {
return exitCode;
}

};
18 changes: 13 additions & 5 deletions ext/miniscript/src/miniscript/miniscript/MiniScript.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include <miniscript/miniscript/MiniScript.h>

#if defined(_MSC_VER)
#include <windows.h>
#endif

#include <algorithm>
#include <filesystem>
#include <fstream>
Expand Down Expand Up @@ -125,6 +129,10 @@ const string MiniScript::Variable::TYPENAME_SET = "Set";
const vector<string> MiniScript::Method::CONTEXTFUNCTIONS_ALL = {};

void MiniScript::initialize() {
//
#if defined(_MSC_VER)
SetConsoleOutputCP(65001);
#endif
//
registerDataType(new HTTPDownloadClientClass());
//
Expand Down Expand Up @@ -785,7 +793,7 @@ bool MiniScript::createStatementSyntaxTree(const string_view& methodName, const
if (viewIsVariableAccess(argument) == true) {
//
Variable value;
value.setValue(deescape(argument));
value.setValue(deescape(argument, statement));

// look up getVariable method
string methodName = argumentIdx >= argumentReferences.size() || argumentReferences[argumentIdx] == false?"getVariable":"getVariableReference";
Expand All @@ -805,7 +813,7 @@ bool MiniScript::createStatementSyntaxTree(const string_view& methodName, const

syntaxTree.arguments.emplace_back(
SyntaxTreeNode::SCRIPTSYNTAXTREENODE_EXECUTE_METHOD,
MiniScript::Variable(deescape(methodName)),
MiniScript::Variable(deescape(methodName, statement)),
method,
initializer_list<SyntaxTreeNode>
{
Expand Down Expand Up @@ -853,7 +861,7 @@ bool MiniScript::createStatementSyntaxTree(const string_view& methodName, const
_StringTools::viewEndsWith(argument, "'") == true)) {
//
Variable value;
value.setValue(deescape(_StringTools::viewSubstring(argument, 1, argument.size() - 1)));
value.setValue(deescape(_StringTools::viewSubstring(argument, 1, argument.size() - 1), statement));
//
syntaxTree.arguments.emplace_back(
SyntaxTreeNode::SCRIPTSYNTAXTREENODE_LITERAL,
Expand All @@ -880,15 +888,15 @@ bool MiniScript::createStatementSyntaxTree(const string_view& methodName, const
// try first user functions
if (functionIdx != SCRIPTIDX_NONE) {
syntaxTree.type = SyntaxTreeNode::SCRIPTSYNTAXTREENODE_EXECUTE_FUNCTION;
syntaxTree.value.setValue(deescape(methodName));
syntaxTree.value.setValue(deescape(methodName, statement));
syntaxTree.setFunctionScriptIdx(functionIdx);
//
return true;
} else
// try methods next
if (method != nullptr) {
syntaxTree.type = SyntaxTreeNode::SCRIPTSYNTAXTREENODE_EXECUTE_METHOD;
syntaxTree.value.setValue(deescape(methodName));
syntaxTree.value.setValue(deescape(methodName, statement));
syntaxTree.setMethod(method);
//
return true;
Expand Down
Loading

0 comments on commit 39d25ed

Please sign in to comment.