Skip to content

Commit

Permalink
need to figure out how to deal with strings
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinbui904 committed Feb 15, 2022
1 parent 33a0b80 commit 8ce8e09
Show file tree
Hide file tree
Showing 43 changed files with 1,055 additions and 1 deletion.
Binary file modified assignment10-starter/linkedlist
Binary file not shown.
2 changes: 1 addition & 1 deletion assignment10-starter/linkedlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,4 @@ void cleanup(Value *list)
free(list);
break;
}
}
}
Binary file removed assignment11-starter/linkedlist
Binary file not shown.
32 changes: 32 additions & 0 deletions assignment12-starter/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Change "no" to "yes" to use Dave's binaries
USE_BINARIES = no

ifeq ($(USE_BINARIES),yes)
SRCS = lib/linkedlist.o lib/talloc.o main.c tokenizer.c
HDRS = tokenizer.h lib/linkedlist.h lib/talloc.h lib/value.h
else
SRCS = linkedlist.c talloc.c main.c tokenizer.c
HDRS = tokenizer.h linkedlist.h talloc.h value.h
endif

CC = clang
CFLAGS = -g

OBJS = $(SRCS:.c=.o)

.PHONY: interpreter
interpreter: $(OBJS)
$(CC) $(CFLAGS) $^ -o $@
rm -f *.o
rm -f vgcore.*

.PHONY: phony_target
phony_target:

%.o : %.c $(HDRS) phony_target
$(CC) $(CFLAGS) -c $< -o $@

clean:
rm -f *.o
rm -f interpreter

Binary file added assignment12-starter/interpreter
Binary file not shown.
164 changes: 164 additions & 0 deletions assignment12-starter/linkedlist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
linkedlist.c
Written by Victor Huang and Thien K. M. Bui
Last modified 02/12/22
*/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#include "linkedlist.h"
#include "talloc.h"
// Create a pointer to a new NULL_TYPE Value (hint: where in memory will
// the Value have to live?)
Value *makeNull()
{
Value *new_null = talloc(sizeof(Value));
new_null->type = NULL_TYPE;

return new_null;
}

// Return whether the given pointer points at a NULL_TYPE Value. Use assertions
// to make sure that this is a legitimate operation. See the assignment
// instructions for further explanation on assertions.
bool isNull(Value *value)
{
if (value->type == NULL_TYPE)
{
return true;
}
return false;
}

// Create a pointer to a new CONS_TYPE Value
Value *cons(Value *newCar, Value *newCdr)
{
Value *new_value = talloc(sizeof(Value));
new_value->type = CONS_TYPE;

struct ConsCell new_cons_cell;
new_cons_cell.car = newCar;
new_cons_cell.cdr = newCdr;

new_value->c = new_cons_cell;
return new_value;
}

// Return a pointer to the car value for the cons cell at the head of the given
// linked list. Use assertions here to make sure that this is a legitimate operation
// (e.g., there is no car value at the head of an empty list). See the assignment
// instructions for further explanation.
Value *car(Value *list)
{
// if not empty
if (list->type == CONS_TYPE)
{
return list->c.car;
}
else if (list->type == NULL_TYPE)
{
return list;
}
// error, should never be of type int/null here
else
{
assert(false != true && "Incorrect type, something went wrong --kb");
return list;
}
}

// Return a pointer to the cdr value for the cons cell at the head of the given linked
// list. Again use assertions to make sure that this is a legitimate operation.
Value *cdr(Value *list)
{
// if not empty
if (list->type == CONS_TYPE)
{
return list->c.cdr;
}
else
{
assert(false != true && "invalid operation on empty list --kb");
return list;
}
}

// Display the contents of the linked list to the screen in the
// format of a Scheme list -- e.g., ( 33 "lol" 9.9 ). It's okay
// to just use printf here, though you'll have to add the quotes in
// yourself, for strings.
void display(Value *list)
{
bool at_end = false;
Value *current_value = list;

printf("( ");
while (!at_end)
{

if (current_value->type == CONS_TYPE)
{
// copy over whatever addresses are in the current ConsCell, it'll get deleted anyway so it doesn't matter
struct ConsCell printed_value = current_value->c;
// check the type of the car cell (Value typed pointer)
switch (printed_value.car->type)
{
case INT_TYPE:
printf("%i ", printed_value.car->i);
break;
case DOUBLE_TYPE:
printf("%f ", printed_value.car->d);
break;
case STR_TYPE:
printf("\"%s\" ", printed_value.car->s);
break;
default:
printf("%i", printed_value.car->i);
}

current_value = cdr(current_value);
}
else
{
printf(")\n");
at_end = true;
}
}
}

// Return a new list that is the reverse of the one that is passed in. None of
// the values in the original linked list should be copied this time. Instead,
// create a new linked list of CONS_TYPE nodes whose car values point to the
// corresponding car values in the original list.
Value *reverse(Value *list)
{
// copy over the current values into heap
Value *current = list;
Value *reverse_list = makeNull();

// at the last node of the list
while (!isNull(current))
{
reverse_list = cons(car(current), reverse_list);
current = cdr(current);
}
return reverse_list;
}

// Return the length of the given list, i.e., the number of cons cells.
// Use assertions to make sure that this is a legitimate operation.
int length(Value *value)
{

int length = 0;
Value *current = value;
while (!isNull(current))
{
length = length + 1;
current = cdr(current);
}
return length;
}
45 changes: 45 additions & 0 deletions assignment12-starter/linkedlist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <stdbool.h>
#include "value.h"

#ifndef _LINKEDLIST
#define _LINKEDLIST

// Create a pointer to a new NULL_TYPE Value (hint: where in memory will
// the value have to live?)
Value *makeNull();

// Return whether the given pointer points at a NULL_TYPE Value. Use assertions
// to make sure that this is a legitimate operation. See the assignment
// instructions for further explanation on assertions.
bool isNull(Value *value);

// Create a pointer to a new CONS_TYPE Value
Value *cons(Value *newCar, Value *newCdr);

// Return a pointer to the car value for the cons cell at the head of the given
// linked list. Use assertions here to make sure that this is a legitimate operation
// (e.g., there is no car value at the head of an empty list). See the assignment
// instructions for further explanation.
Value *car(Value *list);

// Return a pointer to the cdr value for the cons cell at the head of the given linked
// list. Again use assertions to make sure that this is a legitimate operation.
Value *cdr(Value *list);

// Display the contents of the linked list to the screen in the
// format of a Scheme list -- e.g., ( 33 "lol" 9.9 ). It's okay
// to just use printf here, though you'll have to add the quotes in
// yourself, for strings.
void display(Value *list);

// Return a new list that is the reverse of the one that is passed in. None of
// the values in the original linked list should be copied this time. Instead,
// create a new linked list of CONS_TYPE nodes whose car values point to the
// corresponding car values in the original list.
Value *reverse(Value *list);

// Return the length of the given list, i.e., the number of cons cells.
// Use assertions to make sure that this is a legitimate operation.
int length(Value *list);

#endif
11 changes: 11 additions & 0 deletions assignment12-starter/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <stdio.h>
#include "tokenizer.h"
#include "value.h"
#include "linkedlist.h"
#include "talloc.h"

int main() {
Value *tokensList = tokenize();
displayTokens(tokensList);
tfree();
}
1 change: 1 addition & 0 deletions assignment12-starter/my_test.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
("this is" test)
Binary file added assignment12-starter/talloc
Binary file not shown.
97 changes: 97 additions & 0 deletions assignment12-starter/talloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
talloc.c
Written by Victor Huang and Thien K. M. Bui
Last editted 02-13-2022
*/

#include <stdlib.h>
#include <assert.h>

#include "value.h"
#include "talloc.h"

// store head of talloc list
Value *head_talloc = NULL;

// talloc()

// Replacement for malloc that stores the pointers allocated. It should store
// the pointers in a linked list, and you have license here to duplicate code
// that you wrote for linkedlist.c. To be clear, don't actually call functions
// that are defined in linkedlist.h, because then you'll end up with circular
// dependencies, since you'll be using talloc in linkedlist.c.

// insert at the head since order does not matter
void *talloc(size_t size)
{
// make new PTR Value
Value *new_ptr = malloc(sizeof(Value));
new_ptr->type = PTR_TYPE;
new_ptr->p = malloc(size);

// make new CONS (act as linked nodes) car will be the new head, and cdr will be the previous head
Value *new_cons = malloc(sizeof(Value));
new_cons->type = CONS_TYPE;

// cdr here will point to a NULL_TYPE
if (head_talloc == NULL)
{
Value *null_value = malloc(sizeof(Value));
null_value->type = NULL_TYPE;

new_cons->c.car = new_ptr;
new_cons->c.cdr = null_value;
head_talloc = new_cons;
return new_ptr->p;
}
else
{
// make new PTR type
new_cons->c.car = new_ptr;
new_cons->c.cdr = head_talloc;
head_talloc = new_cons;
return new_ptr->p;
}
}

// helper function to make freeing all associated memories of talloc easy
void cleanup(Value *head)
{
if (head->type == NULL_TYPE)
{
free(head);
}
else if (head->type == PTR_TYPE)
{
free(head->p);
free(head);
}
else
{
cleanup(head->c.car);
cleanup(head->c.cdr);
free(head);
}
}

// Free all pointers allocated by talloc, as well as whatever memory you
// allocated for purposes of maintaining the active list. Hint: consider
// that talloc may be called again after tfree is called...
void tfree()
{
Value *current = head_talloc;

cleanup(current);

head_talloc = NULL;
}

// Replacement for the C function 'exit' that consists of two lines: it calls
// tfree before calling exit. It's useful to have later on, since you'll be able
// to call it to clean up memory and exit your program whenever an error occurs.
// Briefly look up exit to get a sense of what the 'status' parameter does.
void texit(int status)
{
tfree();
exit(status);
}
25 changes: 25 additions & 0 deletions assignment12-starter/talloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdlib.h>
#include "value.h"

#ifndef _TALLOC
#define _TALLOC

// Replacement for malloc that stores the pointers allocated. It should store
// the pointers in a linked list, and you have license here to duplicate code
// that you wrote for linkedlist.c. To be clear, don't actually call functions
// that are defined in linkedlist.h, because then you'll end up with circular
// dependencies, since you'll be using talloc in linkedlist.c.
void *talloc(size_t size);

// Free all pointers allocated by talloc, as well as whatever memory you
// allocated for purposes of maintaining the active list. Hint: consider
// that talloc may be called again after tfree is called...
void tfree();

// Replacement for the C function 'exit' that consists of two lines: it calls
// tfree before calling exit. It's useful to have later on, since you'll be able
// to call it to clean up memory and exit your program whenever an error occurs.
// Briefly look up exit to get a sense of what the 'status' parameter does.
void texit(int status);

#endif
Loading

0 comments on commit 8ce8e09

Please sign in to comment.