Skip to content

Commit 6e9abc6

Browse files
committed
map errno to ps_error
1 parent a9292c1 commit 6e9abc6

5 files changed

Lines changed: 37 additions & 15 deletions

File tree

examples/450-enum.pas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
Procedure TestLowHighPredSucc();
2020
Begin
2121
// should not work! (and does not with FPC) because enumeration values are not implicitly convertible to integer/unsigned
22-
// I := Female;
23-
// WriteLn('I = ', I);
22+
I := Female;
23+
WriteLn('I = ', I, ' (SHOULD NOT WORK!');
2424
// should work! and does with FPC because Ord() is used to explicitly convert the enumeration value to an integer/unsigned
2525
I := Ord('A');
2626
WriteLn('I = ', I, ' (=', 65, ')');

include/ps_error.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ extern "C"
8888
int ps_error_sprintf(char *buffer, size_t len, ps_error error, const char *format, ...);
8989
int ps_error_fprintf(FILE *output, ps_error error, const char *format, ...);
9090

91+
ps_error ps_error_map_errno();
92+
9193
#ifdef __cplusplus
9294
}
9395
#endif

src/ps_error.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@
55
*/
66

77
#include <assert.h>
8-
#include <stdarg.h>
9-
#include <stdio.h>
10-
11-
#include "ps_error.h"
12-
13-
#include <assert.h>
8+
#include <errno.h>
149
#include <stdarg.h>
1510
#include <stdio.h>
1611

@@ -129,4 +124,25 @@ int ps_error_fprintf(FILE *output, ps_error error, const char *format, ...) // N
129124
return n;
130125
}
131126

127+
ps_error ps_error_map_errno()
128+
{
129+
switch (errno)
130+
{
131+
case 0:
132+
return PS_ERROR_NONE;
133+
case ENOMEM:
134+
return PS_ERROR_OUT_OF_MEMORY;
135+
case EOVERFLOW:
136+
return PS_ERROR_OVERFLOW;
137+
case EINVAL:
138+
return PS_ERROR_INVALID_PARAMETERS;
139+
case ERANGE:
140+
return PS_ERROR_OUT_OF_RANGE;
141+
case EDOM:
142+
return PS_ERROR_MATH_NAN_INF;
143+
default:
144+
return PS_ERROR_GENERIC;
145+
}
146+
}
147+
132148
/* EOF */

src/ps_string_heap.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ unsigned int ps_string_heap_get_hash_key(const char *z)
8484
ps_string *ps_string_heap_create(ps_string_heap *heap, const char *z)
8585
{
8686
if (z == NULL)
87+
{
88+
errno = EINVAL;
8789
return NULL;
90+
}
8891
size_t len = strlen(z);
8992
if (len > PS_STRING_MAX_LEN)
9093
{

src/ps_visit_expression.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,12 @@ bool ps_visit_relational_expression(ps_interpreter *interpreter, ps_interpreter_
134134

135135
static ps_token_type relational_operators[] = {
136136
// < <= > >= = <>
137-
PS_TOKEN_LT, PS_TOKEN_LE, PS_TOKEN_GT, PS_TOKEN_GE, PS_TOKEN_EQ, PS_TOKEN_NE,
137+
PS_TOKEN_LT,
138+
PS_TOKEN_LE,
139+
PS_TOKEN_GT,
140+
PS_TOKEN_GE,
141+
PS_TOKEN_EQ,
142+
PS_TOKEN_NE,
138143
};
139144
ps_value left = {.type = &ps_system_none, .data.v = NULL};
140145
ps_value right = {.type = &ps_system_none, .data.v = NULL};
@@ -436,12 +441,8 @@ bool ps_visit_factor(ps_interpreter *interpreter, ps_interpreter_mode mode, ps_v
436441
result->data.s = ps_string_heap_create(interpreter->string_heap, lexer->current_token.value.s);
437442
if (result->data.s == NULL)
438443
{
439-
if (errno == ENOMEM)
440-
interpreter->error = PS_ERROR_OUT_OF_MEMORY;
441-
else if (errno == EOVERFLOW)
442-
interpreter->error = PS_ERROR_OVERFLOW;
443-
else if (errno == EINVAL)
444-
interpreter->error = PS_ERROR_STRING_TOO_LONG;
444+
ps_interpreter_set_message(interpreter, "Failed to create string value: %s", strerror(errno));
445+
interpreter->error = ps_error_map_errno();
445446
TRACE_ERROR("STRING_VALUE")
446447
}
447448
}

0 commit comments

Comments
 (0)