Created
October 1, 2012 15:31
-
-
Save Liutos/3812526 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* main.c | |
* | |
* | |
* | |
* Copyright (C) 2012-10-01 liutos | |
*/ | |
#include "type.h" | |
#include <stdio.h> | |
extern LispObject make_cons(char *); | |
void print_cons_core(LispObject); | |
void print_cons(LispObject); | |
int main(int argc, char *argv[]) | |
{ | |
print_cons(make_cons("(a bc)")); | |
putchar('\n'); | |
return 0; | |
} | |
void print_cons_core(LispObject cons) | |
{ | |
while (cons != NULL) { | |
if (ATOM == cons->car->type) | |
printf("%s", cons->car->symbol_name); | |
else { | |
print_cons(cons->car); | |
} | |
if (cons->cdr != NULL) | |
putchar(' '); | |
cons = cons->cdr; | |
} | |
} | |
void print_cons(LispObject cons) | |
{ | |
putchar('('); | |
print_cons_core(cons); | |
putchar(')'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* parse.c | |
* | |
* | |
* | |
* Copyright (C) 2012-10-01 liutos | |
*/ | |
#include "type.h" | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
char *get_symbol(char *string) | |
{ | |
int i; | |
i = 0; | |
while (string[i] != ' ' && string[i] != '(' && | |
string[i] != ')' && string[i] != '\0') | |
i++; | |
return strndup(string, i); | |
} | |
LispObject make_cons_core(char *string, int *offset) | |
{ | |
int i, sub_offset, step; | |
LispObject head, pre, cur, atom; | |
char *token; | |
i = 0; | |
head = malloc(sizeof(struct LispObject)); | |
head->car = head->cdr = NULL; | |
pre = head; | |
while (string[i] != '\0') { | |
switch (string[i]) { | |
case '(': | |
cur = malloc(sizeof(struct LispObject)); | |
cur->type = CONS; | |
cur->car = make_cons_core(string + i + 1, &sub_offset); | |
cur->cdr = NULL; | |
step = sub_offset; | |
break; | |
case ' ': | |
step = 1; | |
break; | |
case ')': | |
*offset = i + 2; | |
return head->cdr; | |
break; | |
default : | |
token = get_symbol(string + i); | |
atom = malloc(sizeof(struct LispObject)); | |
atom->type = ATOM; | |
atom->symbol_name = token; | |
cur = malloc(sizeof(struct LispObject)); | |
cur->type = CONS; | |
cur->car = atom; | |
cur->cdr = NULL; | |
step = strlen(token); | |
} | |
if (string[i] != ' ') { | |
pre->cdr = cur; | |
pre = cur; | |
} | |
i += step; | |
} | |
return head->cdr; | |
} | |
LispObject make_cons(char *string) | |
{ | |
int i; | |
return make_cons_core(string + 1, &i); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef TYPE_H | |
#define TYPE_H | |
typedef enum { | |
CONS, | |
ATOM, | |
SYMBOL, | |
} LispType; | |
typedef struct LispObject { | |
LispType type; | |
union { | |
char *symbol_name; | |
struct { | |
struct LispObject *car; | |
struct LispObject *cdr; | |
}; | |
}; | |
} *LispObject; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment