Skip to content
Permalink
Browse files

Fix memory leaks.

  • Loading branch information...
halogenandtoast committed Feb 16, 2015
1 parent d56bac0 commit 98b09987506bc12e814fea9393d1f45971b9584c
Showing with 29 additions and 11 deletions.
  1. +1 −0 .gitignore
  2. +4 −1 Makefile
  3. +9 −1 main.c
  4. +5 −0 node.h
  5. +10 −9 parse.y
@@ -4,3 +4,4 @@ ruby
parse.tab.c
parse.tab.h
parse.output
ruby.dSYM
@@ -3,7 +3,7 @@ SRC=main.c parse.tab.c lex.yy.c
all: ruby

ruby: ${SRC}
cc -o ruby ${SRC}
cc -O0 -g -o ruby ${SRC}

lex.yy.c: ruby.l
flex ruby.l
@@ -13,3 +13,6 @@ parse.tab.c: parse.y

clean:
rm -rf ruby lex.yy.c parse.tab.c parse.tab.h parse.output

check:
docker run -v `pwd`:/usr/src/ruby ruby bash -c "make clean && make && (valgrind --leak-check=full --show-reachable=yes ./ruby program.rb)"
10 main.c
@@ -5,15 +5,23 @@
#include "parse.tab.h"

extern FILE* yyin;
extern int yyparse(parser_state*);
extern int yylex_destroy(void);

int main(int argc, char *argv[]) {
parser_state state = { NULL, 0 };

if(argc > 1) {
state.source_file = strdup(argv[1]);
state.source_file = argv[1];
yyin = fopen(argv[1], "r");
}

yyparse(&state);

if(argc > 1) {
fclose(yyin);
}

yylex_destroy();
return EXIT_SUCCESS;
}
5 node.h
@@ -1,4 +1,9 @@
#ifndef _NODE_H_
#define _NODE_H_

typedef struct parser_state {
char *source_file;
int source_line;
} parser_state;

#endif
19 parse.y
@@ -22,10 +22,11 @@
%left tPLUS
%right tEQUAL

%token tSTRING tFLOAT tNUMBER tID tCONSTANT tEQUAL tGT tLT tGTE tLTE tNEQUAL
%token tFLOAT tNUMBER tEQUAL tGT tLT tGTE tLTE tNEQUAL
%token tPLUS tMINUS tMULT tDIV tMOD tEMARK tQMARK tAND tOR tLSBRACE tRSBRACE
%token tLPAREN tRPAREN tLBRACE tRBRACE tAT tDOT tCOMMA tCOLON
%token kCLASS kEND kDEF
%token <sval> tSTRING tCONSTANT tID

%start program

@@ -48,16 +49,16 @@ binary_expression: expression tPLUS expression

assignment: variable tEQUAL expression

class_definition: kCLASS tCONSTANT expressions kEND
class_definition: kCLASS tCONSTANT expressions kEND { free($2); }

method_definition: kDEF tID expressions kEND
| kDEF tID tLPAREN tID tRPAREN expressions kEND
method_definition: kDEF tID expressions kEND { free($2); }
| kDEF tID tLPAREN tID tRPAREN expressions kEND { free($2); free($4); }

method_call: variable tDOT tID
| tCONSTANT tDOT tID tLPAREN tSTRING tRPAREN
| tID tSTRING
method_call: variable tDOT tID { free($3); }
| tCONSTANT tDOT tID tLPAREN tSTRING tRPAREN { free($1); free($3); free($5); }
| tID tSTRING { free($1); free($2); }

variable: tID
| tAT tID
variable: tID { free($1); }
| tAT tID { free($2); }

value: tNUMBER

0 comments on commit 98b0998

Please sign in to comment.
You can’t perform that action at this time.