Compare commits

1 Commits
15 changed files with 40 additions and 159 deletions
-1
View File
@@ -1,4 +1,3 @@
clox
*.lox
obj/
bin/
+7 -27
View File
@@ -1,37 +1,17 @@
CC = gcc
CFLAGS=-g -Wall -DDEBUG
SRCDIR=src
OBJDIR=obj
SRCS=$(wildcard $(SRCDIR)/*.c)
# Substitute all .c with .o from SRCS
OBJS=$(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(SRCS))
SOURCEFILES := $(wildcard *.c)
BINDIR=bin
BIN=$(BINDIR)/clox
SOURCE := $(wildcard *.c)
all: $(BIN)
release: CFLAGS=-Wall -O2
release: clean
release: $(BIN)
$(BIN): $(OBJS) $(BINDIR)
$(CC) $(CFLAGS) $(OBJS) -o $@
$(OBJDIR)/%.o: $(SRCDIR)/%.c $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@
$(BINDIR):
mkdir $@
$(OBJDIR):
mkdir $@
$(SOURCE:.c=.o): $(SOURCE)
@mkdir -p obj
$(CC) $^ -o obj/clox
.PHONY: clean
.PHONY: test
clean:
rm -rf $(BINDIR)/* $(OBJDIR)/*
rm -r obj
test:
$(BIN) test.lox
./obj/clox test.lox
+18
View File
@@ -0,0 +1,18 @@
#include "expr.h"
char* Parenthesize(char*, int, ...);
void VisitBinary(struct binary expr) {
}
void VisitGrouping(struct grouping);
void VisitLiteral(Token);
void VisitUnary(struct unary);
char* Parenthesize(char* operator, int count, ...) {
va_list list;
va_start(list, count);
va_arg(list, Expr);
}
+4 -4
View File
@@ -40,9 +40,9 @@ struct Expr {
} expression;
};
// void VisitBinary(struct binary);
// void VisitGrouping(struct grouping);
// void VisitLiteral(Token);
// void VisitUnary(struct unary);
void VisitBinary(struct binary);
void VisitGrouping(struct grouping);
void VisitLiteral(Token);
void VisitUnary(struct unary);
#endif
+6 -33
View File
@@ -14,7 +14,6 @@ Object* VisitBinaryExpression(Expr*);
int IsTruthy(Object*);
int IsEqual(Object*, Object*);
int ConcatStringObject(Object*, const Object*);
void CheckNumberOperands(TokenType, int, ...);
void PrintObject(Object*);
void PrintObject(Object* o) {
@@ -54,6 +53,7 @@ void Interpret(Expr* exp) {
}
Object* VisitLiteralExpression(Expr* expr) {
printf("Literal: %p\n", (double *) expr->expression.Literal->literal);
return CreateObject(expr->expression.Literal->literal, expr->expression.Literal->type);
}
@@ -66,65 +66,59 @@ Object* VisitGroupingExpression(Expr* expression) {
Object* VisitBinaryExpression(Expr* expression) {
Object* left = Evaluate(expression->expression.Binary.left);
Object* right = Evaluate(expression->expression.Binary.right);
PrintObject(left);
PrintObject(right);
double computed_value;
switch(expression->expression.Binary.op->type) {
case Greater:
CheckNumberOperands(expression->expression.Binary.op->type, 2, left, right);
computed_value = left->value.number > right->value.number;
FreeObject(left);
FreeObject(right);
return CreateObject(&computed_value, TRUE);
//return left > right;
case Greater_Equal:
CheckNumberOperands(expression->expression.Binary.op->type, 2, left, right);
computed_value = left->value.number >= right->value.number;
FreeObject(left);
FreeObject(right);
return CreateObject(&computed_value, TRUE);
//return left >= right;
case Less:
CheckNumberOperands(expression->expression.Binary.op->type, 2, left, right);
computed_value = left->value.number < right->value.number;
FreeObject(left);
FreeObject(right);
return CreateObject(&computed_value, TRUE);
//return left < right;
case Less_Equal:
CheckNumberOperands(expression->expression.Binary.op->type, 2, left, right);
computed_value = left->value.number <= right->value.number;
FreeObject(left);
FreeObject(right);
return CreateObject(&computed_value, TRUE);
//return left <= right;
case Bang_Equal:
CheckNumberOperands(expression->expression.Binary.op->type, 2, left, right);
computed_value = !IsEqual(left, right);
FreeObject(left);
FreeObject(right);
return CreateObject(&computed_value, TRUE);
//return !IsEqual(left, right);
case Equal_Equal:
CheckNumberOperands(expression->expression.Binary.op->type, 2, left, right);
computed_value = IsEqual(left, right);
FreeObject(left);
FreeObject(right);
return CreateObject(&computed_value, TRUE);
//return IsEqual(left, right);
case Minus:
CheckNumberOperands(expression->expression.Binary.op->type, 2, left, right);
left->value.number -= right->value.number;
FreeObject(right);
return left;
//return left - right;
case Slash:
CheckNumberOperands(expression->expression.Binary.op->type, 2, left, right);
left->value.number /= right->value.number;
FreeObject(right);
return left;
//return *((double*)left) / *((double*)right);
case Star:
CheckNumberOperands(expression->expression.Binary.op->type, 2, left, right);
printf("%f * %f = %f\n", left->value.number, right->value.number, left->value.number * right->value.number);
left->value.number *= right->value.number;
FreeObject(right);
return left;
@@ -165,7 +159,6 @@ Object* VisitUnaryExpression(Expr* expression) {
switch (expression->expression.Unary.op->type) {
case Minus:
CheckNumberOperands(expression->expression.Unary.op->type, 1, right);//This needs to "throw" if the function fails.
computed_value = -right->value.number;
FreeObject(right);
c = CreateObject(&computed_value, Number);
@@ -229,16 +222,11 @@ Object* CreateObject(const void* value, TokenType type) {
break;
default:
if (value) {
unsigned int length = strlen(value);
int length = strlen(value);
object->instance = INS_STRING;
object->value.string = calloc(length + 1, sizeof(char));
if (!object->value.string) {
fprintf(stderr, "Faild to calloc %u bytes for a new string Object. %s.\n", length + 1, strerror(errno));
return NULL;
}
memcpy(object->value.string, value, length);
strncpy(object->value.string, value, length);
} else object->instance = INS_NULL; //TODO: this is most likely an error, but we'll ignore that for now.
}
@@ -294,19 +282,4 @@ int IsEqual(Object* a, Object* b) {
}
return 0;
}
void CheckNumberOperands(TokenType operator, int operandCount, ...) {
va_list list;
va_start(list, operandCount);
for(int i = 0; i < operandCount; i++) {
Object* operand = va_arg(list, Object*);
if (operand->instance != INS_DOUBLE) {
va_end(list);
return; //TODO: "throw" runtime error "Operand must be a number."
}
}
va_end(list);
}
View File
+1 -2
View File
@@ -39,9 +39,8 @@ void RunFile(const char* path) {
char* contents = GetFileContents(path);
TokenList* tokens = ScanTokens(contents);
free(contents);
printf("TOKENS:\n");
Print(tokens);
printf("EXPRESSION TREE:\n");
printf("TREE:\n");
Expr* tree = GenerateExpressionTree(tokens);
PrintExpressionTree(tree);
printf("\n");
+1 -36
View File
@@ -1,6 +1,5 @@
#include "parser.h"
#include "expr.h"
#include "statement.h"
#include "token.h"
#include <stdio.h>
@@ -11,9 +10,6 @@ Expr* Term(void);
Expr* Factor(void);
Expr* Unary(void);
Expr* Primary(void);
Stmt* Statement(void);
Stmt* PrintStatement(void);
Stmt* ExpressionStatement(void);
int Match(int, ...);
int Check(TokenType);
int ParserAtEnd(void);
@@ -36,34 +32,6 @@ Expr* Expression() {
return Equality();
}
Stmt* Statement(void) {
if (Match(1, PRINT)) return PrintStatement();
return ExpressionStatement();
}
Stmt* PrintStatement(void) {
Expr* value = Expression();
if (!Match(1, Semicolon)) fprintf(stderr, "Expected ';' after value\n");
AdvanceParser();
return CreateStatement(value, STMT_Print);
}
Stmt* ExpressionStatement(void) {
Expr* expr = Expression();
if (!Match(1, Semicolon)) fprintf(stderr, "Expected ';' after expression\n");
AdvanceParser();
return CreateStatement(expr, STMT_Expression);
}
Stmt* ExpressionStatement(void);
Expr* Equality() {
Expr* expr = Comparison();
@@ -178,7 +146,7 @@ Expr* Primary() {
return temp;
}
fprintf(stderr, "Bad expression, this should be unreachable.\n");
printf("Bad expression\n");
return NULL;
}
@@ -189,14 +157,11 @@ int Match(int count, ...) {
for(int i = 0; i < count; i++) {
if(Check(va_arg(list, TokenType))) {
va_end(list);
AdvanceParser();
return 1;
}
}
va_end(list);
return 0;
}
-1
View File
@@ -4,7 +4,6 @@
#include "expr.h"
#include "scanner.h"
#include "token.h"
#include "statement.h"
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
+2 -11
View File
@@ -1,6 +1,5 @@
#include "scanner.h"
#include "token.h"
#include <string.h>
const char* source_code;
//Start and Current hold the offsets that index into the string source_code.
@@ -246,17 +245,9 @@ void ParseNumber(TokenList* list) {
}
snprintf(lexeme, current - start, "%s", &source_code[start]);
double* value = calloc(1, sizeof(double));
if (!value) {
fprintf(stderr, "Failed to calloc for number value. %s\n", strerror(errno));
free(lexeme);
return;
}
*value = atof(lexeme);
double value = atof(lexeme); //Will this reference become stale on return?
AddToTokenList(CreateToken(lexeme, value, line, Number), list);
AddToTokenList(CreateToken(lexeme, &value, line, Number), list);
}
char PeekNext() {
View File
-21
View File
@@ -1,21 +0,0 @@
#include "statement.h"
Stmt* CreateStatement(Expr* expression, StatementType type) {
Stmt* stmt = calloc(1, sizeof(Stmt));
if (!stmt) {
fprintf(stderr, "Failed to calloc space for Statement. %s.\n", strerror(errno));
return NULL;
}
stmt->expression = expression;
stmt->type = type;
return stmt;
}
void FreeStatement(Stmt* stmt) {
if (!stmt) return;
free(stmt);
}
-22
View File
@@ -1,22 +0,0 @@
#ifndef STATEMENT_H
#define STATEMENT_H
#include "expr.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
typedef enum {
STMT_Expression,
STMT_Print
} StatementType;
typedef struct stmt {
StatementType type;
Expr* expression;
} Stmt;
Stmt* CreateStatement(Expr*, StatementType);
void FreeStatement(Stmt*);
#endif
+1 -1
View File
@@ -33,7 +33,7 @@ Token* CreateToken(const char* lexeme, void* literal, int line, TokenType type)
const char* mapping_result = GetLexemeMapping(type);
if (!mapping_result) {
fprintf(stderr, "Failed to get the mapping for TokenType value %d.\n", type);
fprintf(stderr, "Failed to get the mapping for %s\n", lexeme);
free(token);
return NULL;
}
View File