瀏覽代碼

commit lexer

main
leo 3 年之前
父節點
當前提交
8780ca1e54
共有 3 個文件被更改,包括 249 次插入0 次删除
  1. 二進制
      lexer
  2. +170
    -0
      lexer.c
  3. +79
    -0
      main.c

二進制
lexer 查看文件


+ 170
- 0
lexer.c 查看文件

@ -7,6 +7,7 @@ typedef struct Exploration Exploration;
enum opcode {
<<<<<<< HEAD
String,
And,
Or,
@ -16,6 +17,17 @@ enum opcode {
Question_mark,
Exclamation_mark,
Dot
=======
STRING,
AND,
OR,
STAR,
PLUS,
CLASS,
QUESTION_MARK,
EXCLAMATION_MARK,
DOT
>>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee
};
struct Exploration
{
@ -53,6 +65,17 @@ int currentChar(Exploration *exploration)
{
return exploration->text[exploration->position];
}
<<<<<<< HEAD
=======
char *currentText(Exploration *exploration)
{
return exploration->text + exploration->position;
}
struct String { enum opcode type; char *stringValue; int len;};
struct Class { enum opcode type; char *stringValue; int len;};
>>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee
char *currentText(Exploration *exploration)
{
@ -71,6 +94,7 @@ struct Dot { enum opcode type;};
union Node
{
enum opcode type;
<<<<<<< HEAD
struct String String;
struct Class Class;
struct And And;
@ -80,6 +104,13 @@ union Node
struct Question_mark Question_mark;
struct Exclamation_mark Exclamation_mark;
struct Dot Dot;
=======
union {
struct String STRING;
struct Class CLASS;
Node *children[2];
};
>>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee
};
@ -94,9 +125,15 @@ Node *mkNode(size_t size,enum opcode type)
Node *mkString(char *value)
{
<<<<<<< HEAD
Node *node= new(String);
node->String.stringValue= value;
node->String.len=strlen(value);
=======
Node *node= mkNode(STRING);
node->STRING.stringValue= value;
node->STRING.len=strlen(value);
>>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee
return node;
}
@ -146,6 +183,7 @@ Node *mkQuestionMark(Node *child)
return node;
}
<<<<<<< HEAD
Node *mkExclamationMark(Node *child)
{
Node *node= new(Exclamation_mark);
@ -161,6 +199,41 @@ Node *mkDot()
if (node==0){
node=new(Dot);
}
=======
Node *mkOr(Node *node1, Node *node2)
{
Node *node= mkNode(OR);
node->children[0]= node1;
node->children[1]= node2;
return node;
}
Node *mkClass(char* str)
{
Node *node= mkNode(CLASS);
node->STRING.stringValue= str;
node->STRING.len=strlen(str);
return node;
}
Node *mkQuestionMark(Node *child)
{
Node *node= mkNode(QUESTION_MARK);
node->children[0]= child;
return node;
}
Node *mkExclamationMark(Node *child)
{
Node *node= mkNode(EXCLAMATION_MARK);
node->children[0]= child;
return node;
}
Node *mkDot()
{
Node *node= mkNode(DOT);
>>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee
return node;
}
@ -178,12 +251,21 @@ const char* getTypeName(enum opcode type)
{
switch (type)
{
<<<<<<< HEAD
case Star: return "Star";
case Exclamation_mark: return "NOT";
case Dot: return "Dot";
case Question_mark: return "QMARK";
case Plus : return "Plus";
case Class : return "Class";
=======
case STAR: return "STAR";
case EXCLAMATION_MARK: return "NOT";
case DOT: return "DOT";
case QUESTION_MARK: return "QMARK";
case PLUS : return "PLUS";
case CLASS : return "CLASS";
>>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee
default : printf("unexpected use");return 0;
abort();
}
@ -197,6 +279,7 @@ void println(Node *node, int indent)
}
indent++;
switch (node->type) {
<<<<<<< HEAD
case String: printf("%.*s", get(node, String, len), get(node, String, stringValue)); return;
case And: {
printf("AND\n");
@ -233,6 +316,31 @@ void println(Node *node, int indent)
}
printf("Unexpected type %i",node->type);
=======
case STRING: printf("%.*s", get(node, STRING, len), get(node, STRING, stringValue)); return;
case AND: {
printf("AND\n");
println(node->children[0],indent);printf("\n");
println(node->children[1],indent);printf("\n");return;
}
case OR: {
printf("OR\n");
println(node->children[0],indent);printf("\n");
println(node->children[1],indent);printf("\n");return;
}
case DOT:{
printf("DOT\n");return;
}
case CLASS:{
printf("CLASS : Char in [%.*s]\n",get(node, CLASS, len),get(node, CLASS, stringValue));return;
}
default : {
printf("%s\n",getTypeName(node->type));
println(node->children[0],indent);printf("\n");return;
}
}
abort();
>>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee
}
@ -240,6 +348,7 @@ void println(Node *node, int indent)
int execute(Node *node, Exploration *in)
{
switch (node->type) {
<<<<<<< HEAD
case String: {
if (strncmp(currentText(in), get(node,String,stringValue), get(node,String,len))) {
return 0;
@ -281,11 +390,55 @@ int execute(Node *node, Exploration *in)
return 0;
}
if (strchr(get(node,Class,stringValue), currentChar(in))) {
=======
case STRING: {
if (strncmp(currentText(in), node->STRING.stringValue, node->STRING.len)) {
return 0;
}
advance(in, node->STRING.len);
return 1;
}
case AND: {
int pos= getPosition(in);
if (!execute(node->children[0], in)) //si il y a eu une erreur
{
return 0;
} //si ça s'est bien passé
if (!execute(node->children[1], in)) {
setPosition(in, pos);
return 0;
}
return 1;
}
case OR: {
if (execute(node->children[0], in)) {
return 1;
}
return execute(node->children[1], in);
}
case STAR: {
while (execute(node->children[0], in));
return 1;
}
case PLUS: {
if (!execute(node->children[0], in)) {
return 0;
}
while (execute(node->children[0], in));
return 1;
}
case CLASS: {
if (!currentChar(in)) {
return 0;
}
if (strchr(node->STRING.stringValue, currentChar(in))) {
>>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee
advance(in, 1);
return 1;
}
return 0;
}
<<<<<<< HEAD
case Question_mark: {
execute(get(node,Question_mark,children[0]), in);
return 1;
@ -293,12 +446,25 @@ int execute(Node *node, Exploration *in)
case Exclamation_mark: {
int pos= getPosition(in);
if (!execute(get(node,Exclamation_mark,children[0]), in)) {
=======
case QUESTION_MARK: {
execute(node->children[0], in);
return 1;
}
case EXCLAMATION_MARK: {
int pos= getPosition(in);
if (!execute(node->children[0], in)) {
>>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee
return 1;
}
setPosition(in, pos);
return 0;
}
<<<<<<< HEAD
case Dot: {
=======
case DOT: {
>>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee
if (atEnd(in)) {
return 0;
}
@ -332,7 +498,11 @@ int main(int argc, char **argv)
mkStar(mkString("c"))));*/
<<<<<<< HEAD
Node *program= mkOr(mkString("don't match pls"),mkStar(mkDot()));
=======
Node *program= mkStar(mkClass("abc"));
>>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee
Node *program3=mkAnd(mkAnd(mkPlus(mkString("hello world")),mkExclamationMark(mkString("how are u world !?"))),mkStar(mkClass("abcde")));
if (!execute(program, in) || !atEnd(in))

+ 79
- 0
main.c 查看文件

@ -0,0 +1,79 @@
// 1. put a real object system inside the tree-based parsing stuff
// 2. implement ? operator (zero or one of something)
// 3. implement . leaf type (any valid character, NOT the end of the input)
// 4. implement ! operator (prefix, inverts the subexpression) !"abc" matches not "abc" without moving input
// 5. make a function to print the tree describing the pattern
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef union Object Object;
typedef union Object *oop;
enum type {
Integer,
String,
};
struct Integer { enum type type; int i; };
struct String { enum type type; char *s; int len; };
union Object {
enum type type;
struct Integer Integer;
struct String String;
};
oop mkObject(size_t size, enum type type)
{
oop o= calloc(1, size);
o->type = type;
return o;
}
oop _checktype(oop object, enum type type)
{
if (object->type == type) return object;
fprintf(stderr, "\naccesing type %i as if it were a %i\n", object->type, type);
exit(1);
return 0;
}
#define new(type) mkObject(sizeof(struct type), type)
#define get(object, type, member) (_checktype(object, type)->type.member)
oop mkInt(int i)
{
oop o= new(Integer);
o->Integer.i = i;
return o;
}
oop mkStr(char *s)
{
oop o= new(String);
o->String.s = s;
o->String.len = strlen(s);
return o;
}
void println(oop o)
{
switch (o->type) {
case Integer: printf("%i\n", get(o, Integer, i)); return;
case String: printf("%.*s\n", get(o, String, len), get(o, String, s)); return;
}
abort();
}
int main(int argc, char **argv)
{
println(mkInt(42));
println(mkStr("hello world"));
return 0;
}

Loading…
取消
儲存