<commit_before>/*
 * <NAME>'s calculator
 * Copyright (C) 2015-2020 by <NAME> <<EMAIL>>
 * Released under The MIT License
 *
 * This is an educational program that performs simple calculations typed
 * in by the user. It contains a simple tokenizer and recursive descent
 * parser with look-ahead.
 */

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>

#define MAX_LINE 512	/* Maximum input line length */
#define MAX_VAR_NAME 16	/* Maximum variable name length */

#define TOK_NULL	0x00
#define TOK_NUM		0x01
#define TOK_VAR		0x02
#define TOK_OP		0x03
#define TOK_PAREN	0x04
#define TOK_ENDPAREN	0x05
#define TOK_EQUAL	0x06
#define TOK_COMPARE	0x07

#define TOK_FORCE_EXPR	0x7d
#define TOK_INVALID	0x7e
#define TOK_EOL		0x7f

#ifdef DEBUG
/*** Debugging struct ***/
struct toktypename {
	int type;
	char name[24];
};

static const struct toktypename toktypename[] = {
	{ 0x00, "TOK_NULL" },
	{ 0x01, "TOK_NUM" },
	{ 0x02, "TOK_VAR" },
	{ 0x03, "TOK_OP" },
	{ 0x04, "TOK_PAREN" },
	{ 0x05, "TOK_ENDPAREN" },
	{ 0x06, "TOK_EQUAL" },
	{ 0x07, "TOK_COMPARE" },
	{ 0x7d, "TOK_FORCE_EXPR" },
	{ 0x7e, "TOK_INVALID" },
	{ 0x7f, "TOK_EOL" },
	{ 0xff, "" }
};
/*** End debug struct ***/
#endif /* DEBUG */

#define IS_SPACE(a) (a == ' ' || a == '\t')
#define IS_DIGIT(a) (a >= '0' && a <= '9')
#define IS_ALPHA(a) (a >= 'a' && a <= 'z')
#define IS_EQUAL(a) (a == '=')
#define IS_OP(a) (a == '+' || a == '/' || a == '*' || \
		a == '-' || a == '^' || a == '%')

struct var {
	char name[MAX_VAR_NAME];
	long value;
	struct var *next;
} *var_head;


static inline void do_help(void)
{
	fprintf(stderr, "\nType math stuff and get answers!\n");
	fprintf(stderr, "Supports: +, -, /, *, ^, %%, (), integers only\n");
	fprintf(stderr, "For example, type in:\n");
	fprintf(stderr, "123 + 321\n");
	fprintf(stderr, "You'll get the answer:\n");
	fprintf(stderr, "444\n\n");
	fprintf(stderr, "Type 'quit' to exit the program.\n\n");
	return;
}


/* Locate a variable in the variable list */
static struct var *find_var(char *name, struct var **tail)
{
	struct var *p = var_head;

	while (p != NULL) {
		if (!strncmp(p->name, name, MAX_VAR_NAME))
			return p;
		if (tail != NULL) *tail = p;
		p = p->next;
	}
	return NULL;
}


/* Retrieve a variable's value (returns 1 if successful) */
static int get_var(char *name, long *var)<commit_msg>Help text should print to stdout<commit_after>/*
 * <NAME>'s calculator
 * Copyright (C) 2015-2020 by <NAME> <<EMAIL>>
 * Released under The MIT License
 *
 * This is an educational program that performs simple calculations typed
 * in by the user. It contains a simple tokenizer and recursive descent
 * parser with look-ahead.
 */

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>

#define MAX_LINE 512	/* Maximum input line length */
#define MAX_VAR_NAME 16	/* Maximum variable name length */

#define TOK_NULL	0x00
#define TOK_NUM		0x01
#define TOK_VAR		0x02
#define TOK_OP		0x03
#define TOK_PAREN	0x04
#define TOK_ENDPAREN	0x05
#define TOK_EQUAL	0x06
#define TOK_COMPARE	0x07

#define TOK_FORCE_EXPR	0x7d
#define TOK_INVALID	0x7e
#define TOK_EOL		0x7f

#ifdef DEBUG
/*** Debugging struct ***/
struct toktypename {
	int type;
	char name[24];
};

static const struct toktypename toktypename[] = {
	{ 0x00, "TOK_NULL" },
	{ 0x01, "TOK_NUM" },
	{ 0x02, "TOK_VAR" },
	{ 0x03, "TOK_OP" },
	{ 0x04, "TOK_PAREN" },
	{ 0x05, "TOK_ENDPAREN" },
	{ 0x06, "TOK_EQUAL" },
	{ 0x07, "TOK_COMPARE" },
	{ 0x7d, "TOK_FORCE_EXPR" },
	{ 0x7e, "TOK_INVALID" },
	{ 0x7f, "TOK_EOL" },
	{ 0xff, "" }
};
/*** End debug struct ***/
#endif /* DEBUG */

#define IS_SPACE(a) (a == ' ' || a == '\t')
#define IS_DIGIT(a) (a >= '0' && a <= '9')
#define IS_ALPHA(a) (a >= 'a' && a <= 'z')
#define IS_EQUAL(a) (a == '=')
#define IS_OP(a) (a == '+' || a == '/' || a == '*' || \
		a == '-' || a == '^' || a == '%')

struct var {
	char name[MAX_VAR_NAME];
	long value;
	struct var *next;
} *var_head;


static inline void do_help(void)
{
	printf("\nType math stuff and get answers!\n");
	printf("Supports: +, -, /, *, ^, %%, (), integers only\n");
	printf("For example, type in:\n");
	printf("123 + 321\n");
	printf("You'll get the answer:\n");
	printf("444\n\n");
	printf("Type 'quit' to exit the program.\n\n");
	return;
}


/* Locate a variable in the variable list */
static struct var *find_var(char *name, struct var **tail)
{
	struct var *p = var_head;

	while (p != NULL) {
		if (!strncmp(p->name, name, MAX_VAR_NAME))
			return p;
		if (tail != NULL) *tail = p;
		p = p->next;
	}
	return NULL;
}


/* Retrieve a variable's value (returns 1 if successful) */
static int get_var(char *name, long *var)
{'fraction_non_alphanumeric': 0.18076109936575052, 'fraction_numerical': 0.037209302325581395, 'mean_word_length': 3.9025906735751295, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2579154', 'n_tokens_mistral': 2088, 'n_tokens_neox': 1937, 'n_words': 533}