aboutsummaryrefslogtreecommitdiff
path: root/src/ls_kw_impl_PRINT.c
blob: 9c137b4f95ec3910c865bf9a0ad3da89de226bff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// This software disclaims copyright. Do what you want with it. Be gay, do
// crime. Originally written by Alexis Lockwood in 2021. Ⓐ

// --- DEPENDENCIES ------------------------------------------------------------

// Supporting modules
#include "ls_internal.h"
#include "ls_expr.h"
#include "ls_kws.h"
#include "ls_lex.h"

// Standard headers
#include <stdbool.h>
#include <stddef.h>
#include <inttypes.h>

// --- PRIVATE MACROS ----------------------------------------------------------
// --- PRIVATE DATATYPES -------------------------------------------------------
// --- PRIVATE CONSTANTS -------------------------------------------------------
// --- PRIVATE FUNCTION PROTOTYPES ---------------------------------------------

static void _print_value(FILE * f, ls_value_t const * value);

// --- PUBLIC VARIABLES --------------------------------------------------------
// --- PRIVATE VARIABLES -------------------------------------------------------
// --- PUBLIC FUNCTIONS --------------------------------------------------------

void ls_kw_fun_PRINT(ls_t * self)
{
	ls_token_t tok;
	ls_value_t val;
	bool print_lf = true;
	FILE * file = stdout;

	// TODO; implement TO kw

	for (;;)
	{
		ls_lex(self, &tok);

		switch (tok.ty)
		{
		case LS_TOK_STRING:
			for (ls_addr_t i = tok.body.string_val.pc_start;
			     i <= tok.body.string_val.pc_end;
			     i++)
			{
				unsigned char c = ls_fetch(self, i);
				fprintf(file, "%c", c);
			}
			break;

		case LS_TOK_COMMA:
			print_lf = false;
			break;

		case LS_TOK_STATEMENT_SEP:
		case LS_TOK_NONE:
			goto done;

		default:
			print_lf = true;
			ls_eval_expr(self, &val, &tok);
			_print_value(file, &val);
			break;
		}
	}
done:
	if (print_lf)
		fprintf(file, "\n");
}

// --- PRIVATE FUNCTION DEFINITIONS --------------------------------------------

static void _print_value(FILE * f, ls_value_t const * value)
{
	switch (value->ty)
	{
	case LS_TY_INT:
		fprintf(f, "%"PRId32, value->body.integer.value);
		break;
	case LS_TY_STR:
		// TODO
		fprintf(f, "<str>");
		break;
	case LS_TY_LIST:
		// TODO
		fprintf(f, "<list>");
		break;
	case LS_TY_INT_VAR:
	{
		ls_value_t intval;
		intval.ty = LS_TY_INT;
		intval.body.integer.value = value->body.int_var.value;
		_print_value(f, &intval);
		break;
	}
	case LS_TY_VAR:
		_print_value(f, value->body.var.value);
		break;
	default:
		fprintf(f, "<unknown type>");
		break;
	}
}