Skip to content

Instantly share code, notes, and snippets.

@niansa
Last active April 12, 2020 11:14
Show Gist options
  • Save niansa/934d3cab059474c8f1e03cd6b6e599ce to your computer and use it in GitHub Desktop.
Save niansa/934d3cab059474c8f1e03cd6b6e599ce to your computer and use it in GitHub Desktop.
#include <iostream>
#include <map>
#include <cstdio>
#include <cstring>
#include <dlfcn.h>
#include <dirent.h>
#include <interpreter.h>
#include <module_builtin.h>
#include <environment.h>
interpreter::interpreter(int argc, char *argv[])
{
// Initialise environment
environment();
// Load modules from modules directory
struct dirent *dp;
DIR *dfd;
char filename_qfd[264];
char moduledir [] = "modules";
dfd = opendir(moduledir);
while ((dp = readdir(dfd)) != NULL) {
sprintf(filename_qfd , "%s/%s",moduledir,dp->d_name);
// Skip . and ..
if (strcmp(dp->d_name,"..") or strcmp(dp->d_name,"."))
continue;
// Load file
std::cout << "Module loading not yet implemented for: " << filename_qfd << std::endl;
}
// Load builtin modules
module_builtin();
std::list<std::string> testargs = {"hello"};
std::string testcmd = "print";
module_builtin builtin;
builtin.run_command(testcmd, testargs);
interpreter::modules["builtin"] = module_builtin::run_command;
}
#ifndef INTERPRETER_H
#define INTERPRETER_H
#include <rtypes.h>
#include <map>
#include <list>
class interpreter
{
public:
interpreter(int argc, char *argv[]);
protected:
std::map<const char *, rtypes::pilcommand> modules;
private:
};
#endif // INTERPRETER_H
#include "module_builtin.h"
#include <iostream>
#include <list>
std::string module_builtin::cmd_print(std::list<std::string> args) {
for (const std::string & arg : args){
std::cout << arg;
}
std::cout << std::endl;
return "Success";
}
static std::string module_builtin::run_command(std::string command, std::list<std::string> args) {
if (command == "print")
return cmd_print(args);
else
return "No such command";
}
module_builtin::module_builtin() {
}
#ifndef MODULE_BUILTIN_H
#define MODULE_BUILTIN_H
#include <rtypes.h>
#include <map>
#include <list>
#include <string>
class module_builtin
{
public:
module_builtin();
std::string run_command(std::string command, std::list<std::string> args);
std::map<std::string, rtypes::pilcommand> commands;
protected:
private:
};
#endif // MODULE_BUILTIN_H
#include "rtypes.h"
rtypes::rtypes()
{
//ctor
}
#ifndef RTYPES_H
#define RTYPES_H
#include <list>
#include <string>
class rtypes
{
public:
typedef std::string (*pilcommand)(std::list<std::string>);
rtypes();
protected:
private:
};
#endif // RTYPES_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment