Deprecate Char type

This commit is contained in:
Bruce Hill 2024-02-12 00:41:49 -05:00
parent 0f19133534
commit 2c5401aaf7
8 changed files with 6 additions and 103 deletions

View File

@ -36,7 +36,7 @@ libnext.so: metamethods/cord.o util.o SipHash/halfsiphash.o
SipHash/halfsiphash.c:
git submodule update --init --recursive
tags:
tags: $(wildcard **/*.[ch])
ctags **/*.[ch]
clean:
@ -45,4 +45,4 @@ clean:
%.1: %.1.md
pandoc --lua-filter=.pandoc/bold-code.lua -s $< -t man -o $@
.PHONY: all clean install uninstall test
.PHONY: all clean install uninstall test tags

1
ast.c
View File

@ -89,7 +89,6 @@ CORD ast_to_cord(ast_t *ast)
T(Var, "(\x1b[36;1m%s\x1b[m)", data.name)
T(Int, "(\x1b[35m%ld\x1b[m, precision=\x1b[35m%ld\x1b[m)", data.i, data.precision)
T(Num, "(\x1b[35m%ld\x1b[m, precision=\x1b[35m%ld\x1b[m)", data.n, data.precision)
T(Char, "(\x1b[35m'%c'\x1b[m)", data.c)
T(StringLiteral, "\x1b[35m\"%r\"\x1b[m", data.cord)
T(StringJoin, "(%r)", ast_list_to_cord(data.children))
T(Declare, "(var=%s, value=%r)", ast_to_cord(data.var), ast_to_cord(data.value))

5
ast.h
View File

@ -91,7 +91,7 @@ struct type_ast_s {
typedef enum {
Unknown = 0,
Nil, Bool, Var,
Int, Num, Char,
Int, Num,
StringLiteral, StringJoin,
Declare, Assign,
BinaryOp, UnaryOp, UpdateAssign,
@ -136,9 +136,6 @@ struct ast_s {
double n;
enum { NUM_64BIT, NUM_32BIT } precision;
} Num;
struct {
char c;
} Char;
struct {
CORD cord;
} StringLiteral;

View File

@ -1,86 +0,0 @@
#include <gc.h>
#include <gc/cord.h>
#include <stdalign.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/param.h>
#include <err.h>
#include "array.h"
#include "string.h"
#include "types.h"
static CORD Char_cord(const char *c, bool colorize, const TypeInfo *type) {
(void)type;
CORD cord = 0;
switch (*c) {
case '\a': return "\\a";
case '\b': return "\\b";
case '\x1b': return "\\e";
case '\f': return "\\f";
case '\n': return "\\n";
case '\t': return "\\t";
case '\r': return "\\r";
case '\v': return "\\v";
case '\\': return "\\\\";
case '"': return "\\\"";
default: {
if (!isprint(*c))
CORD_sprintf(&cord, "\\x%02X", (int)*c);
else
cord = CORD_cat_char(0, *c);
}
}
if (colorize) {
if (CORD_len(cord) > 1)
CORD_sprintf(&cord, "\x1b[34m%r\x1b[m", cord);
else
CORD_sprintf(&cord, "\x1b[35m%r\x1b[m", cord);
}
return cord;
}
// For some reason, the C functions from ctypes.h return integers instead of
// booleans, and what's worse, the integers are not limited to [0-1]. So,
// it's necessary to cast them to bools to clamp them to those values.
#define BOOLIFY(fn) public bool Char__ ## fn(char c) { return (bool)fn(c); }
BOOLIFY(isalnum)
BOOLIFY(isalpha)
BOOLIFY(iscntrl)
BOOLIFY(isdigit)
BOOLIFY(isgraph)
BOOLIFY(islower)
BOOLIFY(isprint)
BOOLIFY(ispunct)
BOOLIFY(isspace)
BOOLIFY(isupper)
BOOLIFY(isxdigit)
BOOLIFY(isascii)
BOOLIFY(isblank)
typedef bool (*char_pred_t)(char);
typedef char (*char_map_t)(char);
public struct {
TypeInfo type;
char_pred_t isalnum, isalpha, iscntrl, isdigit, isgraph, islower, isprint, ispunct,
isspace, isupper, isxdigit, isascii, isblank;
char_map_t tolower, toupper;
} Char_type = {
.type={
.name="Char",
.size=sizeof(char),
.align=alignof(char),
.tag=CustomInfo,
.CustomInfo={.cord=(void*)Char_cord},
},
.isalnum=Char__isalnum, .isalpha=Char__isalpha, .iscntrl=Char__iscntrl, .isdigit=Char__isdigit, .isgraph=Char__isgraph,
.islower=Char__islower, .isprint=Char__isprint, .ispunct=Char__ispunct, .isspace=Char__isspace, .isupper=Char__isupper,
.isxdigit=Char__isxdigit, .isascii=Char__isascii, .isblank=Char__isblank,
.tolower=(char_map_t)(void*)tolower, .toupper=(char_map_t)(void*)toupper,
};
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0

View File

@ -35,7 +35,6 @@ CORD compile(ast_t *ast)
case Var: return Match(ast, Var)->name;
case Int: return CORD_asprintf("((Int%ld_t)%ld)", Match(ast, Int)->precision, Match(ast, Int)->i);
case Num: return CORD_asprintf(Match(ast, Num)->precision == 64 ? "%g" : "%gf", Match(ast, Num)->n);
case Char: return CORD_asprintf("(char)'\\x%02X'", (int)Match(ast, Char)->c);
case UnaryOp: {
auto unop = Match(ast, UnaryOp);
CORD expr = compile(unop->value);

View File

@ -22,8 +22,6 @@
#define String_t CORD
#define Char_t char
#define Bool_t bool
#define Void_t void

View File

@ -15,7 +15,6 @@ static CORD type_to_cord(type_t *t) {
case VoidType: return "Void";
case MemoryType: return "Memory";
case BoolType: return "Bool";
case CharType: return "Char";
case IntType: return CORD_asprintf("Int%ld", Match(t, IntType)->bits);
case NumType: return CORD_asprintf("Num%ld", Match(t, NumType)->bits);
case ArrayType: {
@ -208,7 +207,7 @@ type_t *type_or_type(type_t *a, type_t *b)
bool is_integral(type_t *t)
{
t = base_variant(t);
return t->tag == IntType || t->tag == CharType;
return t->tag == IntType;
}
bool is_floating_point(type_t *t)
@ -227,7 +226,6 @@ static inline double type_min_magnitude(type_t *t)
{
switch (t->tag) {
case BoolType: return (double)false;
case CharType: return (double)CHAR_MIN;
case IntType: {
switch (Match(t, IntType)->bits) {
case 8: return (double)INT8_MIN;
@ -247,7 +245,6 @@ static inline double type_max_magnitude(type_t *t)
{
switch (t->tag) {
case BoolType: return (double)true;
case CharType: return (double)CHAR_MAX;
case IntType: {
switch (Match(t, IntType)->bits) {
case 8: return (double)INT8_MAX;
@ -416,7 +413,7 @@ bool can_leave_uninitialized(type_t *t)
{
switch (t->tag) {
case PointerType: return Match(t, PointerType)->is_optional;
case ArrayType: case IntType: case NumType: case CharType: case BoolType:
case ArrayType: case IntType: case NumType: case BoolType:
return true;
case StructType: {
for (arg_t *field = Match(t, StructType)->fields; field; field = field->next) {

View File

@ -35,7 +35,6 @@ struct type_s {
VoidType,
MemoryType,
BoolType,
CharType,
IntType,
NumType,
ArrayType,
@ -51,7 +50,7 @@ struct type_s {
union {
struct {
} UnknownType, AbortType, VoidType, MemoryType, BoolType, CharType;
} UnknownType, AbortType, VoidType, MemoryType, BoolType;
struct {
int64_t bits;
} IntType;