Commits

Steven! Ragnarök committed a32e33a

Begin work with new parser grammar and interpreter.

  • Participants
  • Parent commits d9b74e7

Comments (0)

Files changed (4)

File examples/src/test_parser.c

-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include "../include/parser.h"
-
-int main() {
-	char *command = "echo";
-	strlist *tokens = malloc(sizeof(strlist));
-	strncpy(tokens->str, command, MAX_TOKEN_LENGTH);
-	tokens->next =  malloc(sizeof(strlist));
-	strncpy(tokens->next->str, "hello", MAX_TOKEN_LENGTH);
-	tokens->next->next = NULL;
-
-	ast_node *ast = parse(tokens);
-	ast_nodelist *children = ast->children;
-
-	printf("The node type is COMMAND %d\n", (ast->type == COMMAND));
-	printf("The token is %s\n", ast->token);
-	printf("The node has children %d.\n\n", children != NULL);
-	while (children != NULL) {
-		printf("The node type is ARG %d\n", (children->node.type == ARG));
-		printf("The token is %s\n", children->node.token);
-		children = children->next;
-	}
-	return 0;
-}
-

File include/sh142types.h

 
 /* Different types of tokens!!!! */
 typedef enum {
-	VARASSIGN, VALUE, VARIABLE, COMMAND, NEGATED_COMMAND
+	VARASSIGN, VALUE, VARIABLE, COMMAND, NEGATED_COMMAND, ORRED_COMMAND,
+	ANDED_COMMAND
 } node_type;
 
 /* Since we have mutually recursive structures, we need to use forward

File src/interpreter.c

 	if (root->type == VARASSIGN) {
 		exit_status = interpret_var_assign(root);
 	} else {
-		exit_status = interpret_command_seq(root);
+		exit_status = interpret_command(root);
 	}
 
 	fifo_push(MAX_SAVED_EXITSTATUSES, exitstatuses, exitstatus_head, exit_status);
 	return;
 }
 
-int interpret_command_seq(ast_node *command_seq) {
-	int exit_status = 99;
-	switch (command_seq->type) {
-		case NEGATED_COMMAND:
-			exit_status = interpret_negated_command(command_seq);
-			break;
-		case COMMAND:
-			exit_status = interpret_command(command_seq);
-			break;
-	}
-	return exit_status;
-}
-
 int interpret_command(ast_node* command) {
 	int cmdstats, argc = 0, execerr;
 	pid_t cmdpid;
 	char *argv[MAX_TOKEN_LENGTH];
-	ast_nodelist *arg = command->children;
-	
+	ast_nodelist *arg, *arg_list = command->children;
+
 	/* Build the values to pass to execvp */
 	argv[argc] = interpret_value(command);
 	++argc;
+
+	arg = arg_list->node->children;
 	
 	while (arg != NULL) {
 		if (arg->node->type == VARIABLE) {

File src/parser.c

 #include <string.h>
 #include "../include/parser.h"
 
-
 ast_node *parse() {
 	int eql;
+	ast_node *root;
 	if ((eql = contains_eql(tokens->str)) != -1) {
-		return parse_var_assign(eql);
+		root =  parse_var_assign(eql);
 	} else {
-		return parse_command_seq();
+		root = parse_command();
 	}
-}
-
-ast_node *parse_command_seq() {
-	if (strcmp(tokens->str, "!") == 0) {
-		return parse_negated_command();
-	} else {
-		return parse_command();
-	}
+	return root;
 }
 
 ast_node *parse_command() {
 	command->type = COMMAND;
 	strncpy(command->token, tokens->str, MAX_TOKEN_LENGTH);
 	strip_head();
-	command->children = parse_arglist();
+	command->children = malloc(sizeof(ast_nodelist));
+	command->children->node = parse_arg_list();
 	return command;
 }
 
-ast_node *parse_negated_command() {
-	ast_node *negated_command;
+ast_node *parse_arg_list() {
+	ast_node *arg_list = malloc(sizeof(ast_node)), *arg_node;
+	ast_nodelist *head, *tail;
+	head = tail = malloc(sizeof(ast_nodelist));
+	arg_list->children = head;
+	arg_node = parse_arg();
+	tail->node = arg_node;
 	strip_head();
-	negated_command = parse_command_seq();
-	negated_command->type = NEGATED_COMMAND;
-	return negated_command;
-}
 
-
-
-ast_nodelist *parse_arglist() {
-	ast_nodelist *head, *tail;
-	ast_node *arg_node;
-	if (tokens == NULL) {
-		return NULL;
-	}
-	/* Prep the first node */
-	head = tail = malloc(sizeof(ast_nodelist));
-	head->node = parse_arg();
-	strip_head();
-	head->next = NULL;
-
-	/* Prime the while with a new argument */
 	arg_node = parse_arg();
 	strip_head();
 
 		arg_node = parse_arg();
 		strip_head();
 	}
-	return head;
+	return arg_list;
 }
 
 ast_node *parse_arg() {
-	if (tokens == NULL) {
+	if (tokens == NULL || is_special_token()) {
 		return NULL;
 	}
 
 	if (tokens->str[0] == '$') {
-		return parse_variable();
+		return parse_variable_expression();
 	}
 
 	return parse_value();
 	return value;
 }
 
-ast_node *parse_variable() {
+ast_node *parse_variable_expression() {
 	int i, varlen = strlen(tokens->str);
 	ast_node *variable = malloc(sizeof(ast_node));
 	variable->type = VARIABLE;
 			return i;
 		}
 	}
-
 	return -1;
 }
 
+int is_special_token() {
+	if (strcmp(tokens->str, "&&") == 0) {
+		return 1;
+	} else if (strcmp(tokens->str, "||") == 0) {
+		return 1;
+	} else {
+		return 0;
+	}
+}
+
+