CharCode

From Esolang
(Redirected from Onlynumber)
Jump to navigation Jump to search

CharCode is an esoteric programming language written in simple C.

Language Overview

CharCode is a language that uses +, -, <, >, ! , #, *, /, ^, and @ to complete the language, and other characters (not relevant for program execution) can be added in between to increase obfuscation. In addition, the language only allows variables called var, so it is not very easy to perform calculations. The language is not suitable for calculations.

The 11 commands presented here have the following roles.

commands description
+ Add 1 to var
- Subtract 1 from var.
< Multiply var by 10
> Divide var by 10
! The value stored in var is read as a character code and output as a single character. At this time, var is assigned 0.
# Set the value of var to 0
* Add 10 to var
/ Subtract 10 from var
^ Double the value of var and assign it
@ Substitute half of the value of var.
? Set 0 to var

Example

An example code that outputs "HelloWorld" to the terminal is shown below.

+^<**----^!+<<+!+<<*--!+<<*--!+<<*+!+++++<****---!+<<*+!+<<*++++!+<<*--!+<<!***+++!

Note: Character codes used on each PC may differ. Also, this code is an example, and results may differ depending on your environment.

How to use

For C Language

This explanation assumes that the C source code has already been compiled and could be executed. This explanation assumes that the source code has been compiled by using the command "gcc <your-file-name>".

Enter ". /a.out <file name>" in your terminal. The <file name> is the name of the file where the CharCode source code is saved.

The results and an exit status (the content of the "var") are displayed after the program finishes.

Source Code

C Language

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char ** argv) 
{
	if (argc < 2) {
		fprintf(stderr, "Usage: %s <input file>\n", argv[0]);
		exit(1);
	}

	FILE * fp = fopen(argv[1], "r");
	
	if (fp == NULL) {
		fprintf(stderr, "Error: Failed to open the file.\n");
		exit(2);
	}

	int var = 0;

	int no;

	for (int ch; (ch = fgetc(fp)) != EOF;) {
		if (ch == '+') { 
			var++;
		} else if (ch == '-') {
			var--;
		} else if (ch == '<') {
			var = var * 10;
		} else if (ch == '>') {
			var = var / 10;
		} else if (ch == '!') {
			printf("%c", var);
			var = 0;
		} else if (ch == '?') {
			no = scanf("%d", &var);
		} else if (ch == '#') {
			var = 0;
		} else if (ch == '*') {
			var += 10;
		} else if (ch == '/') {
			var -= 10;
		} else if (ch == '^') {
			var *= 2;
		} else if (ch == '@') {
			var /= 2;
		}
	}

	fclose(fp);

	return 0;
}

Python Language

import sys

if len(sys.argv) < 2:
    sys.stderr.write("Usage: {} <input file>\n".format(sys.argv[0]))
    sys.exit(1)

try:
    with open(sys.argv[1], "r") as fp:
        var = 0
        no = 0

        for ch in fp.read():
            if ch == '+':
                var += 1
            elif ch == '-':
                var -= 1
            elif ch == '<':
                var *= 10
            elif ch == '>':
                var //= 10
            elif ch == '!':
                print(chr(var), end="")
                var = 0
            elif ch == '?':
                try:
                    var = int(input())
                except ValueError:
                    sys.stderr.write("Error: Failed to read the input.\n")
                    sys.stdout.write("\n-------------------\nThe Program has not finished correctly. (Exit Status: {})\n".format(var))
                    sys.exit(1)
            elif ch == '#':
                var = 0
            elif ch == '*':
                var += 10
            elif ch == '/':
                var -= 10
            elif ch == '^':
                var *= 2
            elif ch == '@':
                var //= 2

    sys.stdout.write("\n-------------------\nThe Program has finished correctly. (Exit Status: {})\n".format(var))

except IOError:
    sys.stderr.write("Error: Failed to open the file.\n")
    sys.exit(2)

sys.exit(0)