Commits

Steven! Ragnarök committed 9789004

Reimplement negated commands with new parser.

  • Participants
  • Parent commits 60333aa

Comments (0)

Files changed (4)

  * NetworkCommand     ::= NodeName':'Command
  * NodeName           ::= Value
  * DataVariable       ::= #D:Value
+ * BackgroundKey      ::= '&'
  * // Not Yet Implemented //
  * Chain              ::= AndedCommand | OrredCommand
  * AndedCommand       ::= '&&' Command
  * OrredCommand       ::= '||' Command
- * BackgroundKey      ::= '&'
+ * PipedCommand       ::= '|'
  */
 
 strlist *tokens;
 /* Actual parsing functions */
 ast_node *parse(void);
 ast_node *parse_command(void);
+ast_node *parse_negated_command(void);
 ast_node *parse_arg_list(void);
 ast_node *parse_arg(void);
 ast_node *parse_var_assign(int eqlidx);
 ast_node *parse_redirect_list(void);
 ast_node *parse_redirect(void);
 ast_node *parse_network_command(int colonidx);
-ast_node *parse_background_key();
-
+ast_node *parse_background_key(void);
 
 /* Helper functions */
 void strip_head(void);
 int contains_eql(char *str);
 int contains_colon(char *str);
+int is_negation(char *str);
 int is_special_token(void);
 int is_redirect_token(void);
 int is_background_key(void);

include/sh142types.h

 /* Different types of tokens!!!! */
 typedef enum {
 	VARASSIGN, VALUE, VARIABLE, COMMAND, ARGLIST, REDIRECTLIST, STDIN_REDIRECT,
-	STDOUT_REDIRECT, STDERR_REDIRECT, DATA_VARIABLE, BACKGROUND_KEY
+	STDOUT_REDIRECT, STDERR_REDIRECT, DATA_VARIABLE, BACKGROUND_KEY,
+	NEGATED_COMMAND
 } node_type;
 
 /* Since we have mutually recursive structures, we need to use forward

src/interpreter.c

 	int exit_status;
 	if (root->type == VARASSIGN) {
 		exit_status = interpret_var_assign(root);
-	} else {
+	} else if (root->type == NEGATED_COMMAND) {
+		exit_status = interpret_negated_command(root);
+	}else {
 		exit_status = interpret_command(root);
 	}
 
 	}
 }
 
-int interpret_negated_command(ast_node *command) {
+int interpret_negated_command(ast_node *negated_command) {
 	int command_status;
-	command_status = interpret_command(command);
+	command_status = interpret_command(negated_command->children->node);
 	if (command_status == 0) {
 		return 1;
 	} else {
 		root =  parse_var_assign(eql);
 	} else if ((colon = contains_colon(tokens->str)) != -1) {
 		root = parse_network_command(colon);
+	} else if (is_negation(tokens->str)) {
+		root = parse_negated_command();
 	} else {
 		root = parse_command();
 	}
 	return data_variable;
 }
 
+ast_node *parse_negated_command() {
+	ast_node *negated_command = malloc(sizeof(ast_node));
+	negated_command->type = NEGATED_COMMAND;
+	strip_head();
+	negated_command->children = malloc(sizeof(ast_nodelist));
+	negated_command->children->next = NULL;
+	negated_command->children->node = parse_command();
+	return negated_command;
+}
+
 ast_node *parse_var_assign(int eqlidx) {
 	ast_node *var_assign = malloc(sizeof(ast_node));
 	ast_nodelist *varname = malloc(sizeof(ast_nodelist));
 	}
 }
 
+int is_negation(char *str) {
+	if (strcmp(str, "!") == 0) {
+		return 1;
+	} else {
+		return 0;
+	}
+}
+
 void parser_error() {
 	fprintf(stderr, "Parser error\n");
 	return;