Skip to content

Instantly share code, notes, and snippets.

@bakman329
Last active May 5, 2016 12:58
Show Gist options
  • Save bakman329/11272496 to your computer and use it in GitHub Desktop.
Save bakman329/11272496 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
struct node {
double x;
int subscript;
struct node *next;
} *head = NULL, *root = NULL;
struct node *list_create()
{
root = (struct node*)malloc(sizeof(struct node));
head = root;
head->next = NULL;
return root;
}
int list_length()
{
int cnt = 0;
struct node ptr = *root;
while (ptr.next != NULL) {
ptr = *ptr.next;
++cnt;
}
return cnt;
}
struct node *list_get(int location)
{
struct node *ptr;
ptr = root;
for (int ptr_location = 0; ptr_location < location; ++ptr_location)
ptr = ptr->next;
return ptr;
}
struct node *list_push(double x)
{
head->next = (struct node*)malloc(sizeof(struct node));
int old_subscript = head->subscript;
head = head->next;
head->next = NULL;
head->x = x;
head->subscript = old_subscript + 1;
return head;
}
struct node *list_pop()
{
if(list_length() < 1) return NULL;
struct node *prev = list_get(head->subscript - 1);
head = prev;
struct node *old_head_next = (struct node*)malloc(sizeof(struct node));
old_head_next->x = head->next->x;
old_head_next->subscript = head->next->subscript;
free(head->next);
head->next = NULL;
return old_head_next;
}
void list_clear()
{
int length = list_length();
for (int i = 0; i < length; ++i)
list_pop();
}
struct node *list_copy()
{
return list_push(head->x);
}
struct node *list_root()
{
return root;
}
struct node *list_head()
{
return head;
}
struct node *list_add()
{
if (list_length() < 2) return NULL;
struct node *operand0 = list_pop();
struct node *operand1 = list_pop();
return list_push(operand1->x + operand0->x);
}
struct node *list_sub()
{
if (list_length() < 2) return NULL;
struct node *operand0 = list_pop();
struct node *operand1 = list_pop();
return list_push(operand1->x - operand0->x);
}
struct node *list_mul()
{
if (list_length() < 2) return NULL;
struct node *operand0 = list_pop();
struct node *operand1 = list_pop();
return list_push(operand1->x * operand0->x);
}
struct node *list_div()
{
if (list_length() < 2) return NULL;
struct node *operand0 = list_pop();
struct node *operand1 = list_pop();
return list_push(operand1->x / operand0->x);
}
struct node *list_pow()
{
if (list_length() < 2) return NULL;
struct node *operand0 = list_pop();
struct node *operand1 = list_pop();
return list_push(pow(operand1->x, operand0->x));
}
struct node *head_out_char()
{
if (list_length() < 1) return NULL;
printf("%c\n", (int)head->x);
return list_pop();
}
struct node *head_out_float()
{
if (list_length() < 1) return NULL;
printf("%f\n", head->x);
return list_pop();
}
struct node *list_eq()
{
if (list_length() < 2) return NULL;
struct node *operand0 = list_pop();
struct node *operand1 = list_pop();
return list_push(operand0->x == operand1->x);
}
struct node *list_more()
{
if (list_length() < 2) return NULL;
struct node *operand0 = list_pop();
struct node *operand1 = list_pop();
return list_push(operand0->x == operand1->x);
}
struct node *list_less()
{
if (list_length() < 2) return NULL;
struct node *operand0 = list_pop();
struct node *operand1 = list_pop();
return list_push(operand0->x == operand1->x);
}
char list_input()
{
char input;
printf(">>> ");
scanf("%s", &input);
return input;
}
void list_print()
{
struct node ptr = *root;
int cnt = 0;
while (ptr.next != NULL) {
printf("%f", ptr.next->x);
ptr = *ptr.next;
if (ptr.next != NULL) printf(", ");
++cnt;
}
if (cnt > 0) printf("\n");
}
struct node *list_operate_binary(char operation){
if (list_length() < 2) return NULL;
struct node *operand0 = list_pop();
struct node *operand1 = list_pop();
switch(operation){
case '+':
return list_push(operand1->x + operand0->x);
case '-':
return list_push(operand1->x - operand0->x);
case '*':
return list_push(operand1->x * operand0->x);
case '/':
return list_push(operand1->x / operand0->x);
case '^':
return list_push(pow(operand1->x, operand0->x));
case '=':
return list_push(operand1->x == operand0->x);
case '>':
return list_push(operand1->x > operand0->x);
case '<':
return list_push(operand1->x < operand0->x);
default:
return NULL;
}
}
struct node *parse(char token)
{
if (isdigit(token))
return list_push(token - '0');
if (isalpha(token))
return list_push(token);
switch (token) {
case '+':
return list_add();
case '-':
return list_sub();
case '*':
return list_mul();
case '/':
return list_div();
case '^':
return list_pow();
case '.':
return head_out_char();
case ':':
return head_out_float();
case ',':
return list_pop();
case '@':
list_clear();
return NULL;
case '"':
return parse(list_input());
case '=':
return list_eq();
case '>':
return list_more();
case '<':
return list_less();
case '#':
list_print();
return NULL;
case '_':
return list_copy();
default:
return NULL;
}
}
struct node *multi_parse(char *tokens)
{
struct node *ret;
struct node *temp;
for (int i = 0; i < strlen(tokens); i++)
if ((temp = parse(tokens[i])) != NULL) ret = temp;
if (ret != NULL) list_print();
return ret;
}
int main(int argc, char **argv)
{
//list_create();
root = (struct node*)malloc(sizeof(struct node));
head = root;
if (argc < 2){
printf("Usage: ./<program> -[h|i|e] [file]\n");
exit(1);
}
if (strcmp(argv[1], "-e") == 0) {
if (argc != 3) {
printf("Usage: ./<program> -e <expression>\n");
exit(1);
}
char *expression = argv[2];
for (int i = 0; i < argc - 2; i++)
multi_parse(&expression[i]);
} else if (argc == 2) {
if (strcmp(argv[1], "-i") == 0) {
printf("List language interactive prompt\nPress control-C to exit\n");
while (1) {
char input[50];
printf(">> ");
scanf("%49s", input);
multi_parse(input);
}
} else {
FILE *file;
file = fopen(argv[1], "r");
if(file == 0) {
printf("Failed to open file\n");
return 1;
} else {
int char_at_pt = fgetc(file);
do {
parse((char)char_at_pt);
} while((char_at_pt = fgetc(file)) != EOF);
fclose(file);
}
}
}
return 0;
}
#include <iostream>
#include <fstream>
void handle_args(int argc, char **argv) {
if (argc < 2){
help_message();
return 1;
}
else {
handle_options(argv[1]);
}
}
void help_message() {
std::cout << "Usage: ./<program> -[h|i] [file]\n";
}
void shell() {
std::cout << "List language interactive prompt\nPress control-C to exit\n";
while (true) {
std::string input;
std::cout << ">> ";
std::cin >> input;
parse(input);
}
}
void read_file(char *filename) {
std::string line;
ifstream file(filename);
if (myfile.is_open()) {
while (getline(file, line)) {
parse(line);
}
myfile.close();
}
else {
std::cout << "Failed to open file \"" << filename << "\"\n";
}
}
void handle_options(char *option) {
if (strcmp(option, "-h") == 0) {
help_message();
}
else if (strcmp(option, "-i") == 0) {
shell();
}
else {
read_file(option);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment