Skip to content

Commit

Permalink
Change to use 'global' pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
thedavidchu committed Feb 19, 2025
1 parent 0c5b95e commit c6ca0c2
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 131 deletions.
14 changes: 8 additions & 6 deletions interpreter/boolean.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdio.h>
#include <string.h>

#include "global.h"
#include "object.h"

static inline char const *
Expand All @@ -25,28 +26,29 @@ boolean_error(struct Object const *const me)

int
boolean_ctor(struct Object *me,
struct ObjectType const *const type,
struct Global const *const global,
union ObjectData data)
{
if (me == NULL) {
if (me == NULL || global == NULL) {
return -1;
}
me->type = type;
me->type = &global->builtin_types.boolean;
// TODO Check if data is valid.
me->data = data;
return 0;
}

int
boolean_from_cstr(struct Object *const me,
struct ObjectType const *const type,
struct Global const *const global,
char const *const cstr,
char const **cstr_end)
{
if (me == NULL || type == NULL || cstr == NULL) {
if (me == NULL || global == NULL || cstr == NULL) {
return -1;
}
me->type = type;
me->global = global;
me->type = &global->builtin_types.boolean;
if (strncmp(cstr, "true", sizeof("true")) == 0) {
me->data.boolean = true;
*cstr_end = &cstr[sizeof("true")];
Expand Down
2 changes: 2 additions & 0 deletions interpreter/global.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#include "object.h"

struct Global {
Expand Down
9 changes: 5 additions & 4 deletions interpreter/nothing.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <stdbool.h>

Check failure on line 3 in interpreter/nothing.h

View workflow job for this annotation

GitHub Actions / Lint

interpreter/nothing.h:3:10 [clang-diagnostic-error]

'stdbool.h' file not found
#include <stdio.h>

#include "array.h"
#include "global.h"

static int
nothing_error(struct Object const *const me)
Expand All @@ -25,13 +25,14 @@ nothing_error(struct Object const *const me)

int
nothing_ctor(struct Object *me,
struct ObjectType const *const type,
struct Global const *const global,
union ObjectData data)
{
if (me == NULL || type == NULL) {
if (me == NULL || global == NULL) {
return -1;
}
me->type = type;
me->global = global;
me->type = &global->builtin_types.nothing;
// TODO Check if data is valid.
me->data = data;
return 0;
Expand Down
23 changes: 13 additions & 10 deletions interpreter/number.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

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

#include "global.h"
#include "object.h"

static int
number_error(struct Object const *const me)
{
if (me == NULL || me->type == NULL ||
if (me == NULL || me->global == NULL || me->type == NULL ||
me->type->type != OBJECT_TYPE_NUMBER) {
return -1;
}
Expand All @@ -19,13 +19,14 @@ number_error(struct Object const *const me)

int
number_ctor(struct Object *me,
struct ObjectType const *const type,
struct Global const *const global,
union ObjectData data)
{
if (me == NULL) {
if (me == NULL || global == NULL) {
return -1;
}
me->type = type;
me->global = global;
me->type = &global->builtin_types.number;
// TODO Check if data is valid.
me->data = data;
return 0;
Expand Down Expand Up @@ -92,14 +93,15 @@ number_fprint(struct Object const *const me, FILE *const fp, bool const newline)

int
number_from_cstr(struct Object *const me,
struct ObjectType const *const type,
struct Global const *const global,
char const *const cstr,
char const **cstr_end)
{
if (me == NULL || type == NULL || cstr == NULL) {
if (me == NULL || global == NULL || cstr == NULL) {
return -1;
}
me->type = type;
me->global = global;
me->type = &global->builtin_types.number;
if (isspace(*cstr)) {
*cstr_end = cstr;
return -1;
Expand Down Expand Up @@ -148,8 +150,9 @@ generic_number_op(struct Object const *const lhs,
default:
return -1;
}
if ((err =
number_ctor(result, lhs->type, (union ObjectData){.number = ans})))
if ((err = number_ctor(result,
lhs->global,
(union ObjectData){.number = ans})))
;
return 0;
}
Expand Down
Loading

0 comments on commit c6ca0c2

Please sign in to comment.