Skip to content

Instantly share code, notes, and snippets.

@myun2
Last active May 18, 2017 11:25
Show Gist options
  • Save myun2/caf2deb5eeff0234926750aaaa348dbd to your computer and use it in GitHub Desktop.
Save myun2/caf2deb5eeff0234926750aaaa348dbd to your computer and use it in GitHub Desktop.
diamond.c
#include <stdio.h>
#include <vector>
#include <string>
#include <map>
using ::std::vector;
using ::std::map;
using ::std::string;
enum diamond_primitives {
diamond_int,
diamond_float,
diamond_string
};
enum diamond_ope_codes {
diamond_op_type,
diamond_op_class,
diamond_op_interface,
diamond_op_var,
diamond_op_const,
diamond_op_final,
diamond_op_if,
diamond_op_not,
diamond_op_and,
diamond_op_or,
diamond_op_return,
diamond_op_def,
diamond_op_nop
};
typedef vector<int> operands_t;
struct Ope
{
int code;
int id;
operands_t operands;
};
struct DiamondClass
{
int id;
vector<int> variables;
vector<int> variable_types;
};
struct DiamondMethod
{
int id;
vector<int> operations;
};
struct DiamondInstance
{
int id;
int class_id;
vector<DiamondInstance> variables;
};
struct Unit
{
};
struct VM
{
map<string, int> literals;
typedef map<string, int>::iterator literal_iterator;
vector<literal_iterator> literals_vec;
vector<DiamondClass> classes;
vector<DiamondInstance> instances;
vector<DiamondMethod> methods;
vector<Ope> operations;
};
VM vm;
operands_t diamond_parse_line(const char* s)
{
operands_t operands;
char c;
string literal;
while(c = *(s++))
{
// white space
if (c == ' ' || c == ',' || c == '\t' || c == '\n' || c == '\r' || c == '\0' )
{
if (literal.size() == 0) continue;
printf("%s\n", literal.c_str());
auto it = vm.literals.find(literal);
if (it == vm.literals.end()) {
vm.literals[literal] = vm.literals.size();
vm.literals_vec.push_back(vm.literals.find(literal));
}
operands.push_back(vm.literals[literal]);
literal = "";
if (c == '\n' || c == '\r' || c == '\0')
return operands;
continue;
}
literal += c;
}
}
int diamond_parse(FILE* fp)
{
char buf[2048];
while(fgets(buf, 2048, fp))
{
diamond_parse_line(buf);
}
return 0;
}
int main(int argc, const char *argv[])
{
if ( argc != 2 )
{
printf("diamond <filename>\n");
return 1;
}
FILE *fp = fopen(argv[1], "r");
if ( fp == NULL )
{
printf("%s is not found.\n", argv[1]);
return 2;
}
diamond_parse(fp);
fclose(fp);
return 0;
}
all:
g++ -std=c++0x -O3 diamond.cpp -o diamond
run:
./diamond test.dia
class XYZ <T>
var x T
var y T
var z T
def zero?
x == 0 && y == 0 && z == 0
end
def non_zero?
!zero?
end
end
hoge = new XYZ int 0 1 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment