MetaC

From Esolang
Jump to navigation Jump to search
This article is not detailed enough and needs to be expanded. Please help us by adding some more information.
MetaC
Designed by Jannes
Appeared in 2024
Dimensions one-dimensional
Computational class Meta Turing-complete
Reference implementation See #Implementations
Influenced by C, MetaGolfScript
File extension(s) .c

MetaC is a family of esoteric programming languages. Every single language in the family is a version of C.

Overview

The family consists of infinite amount of languages, named metaC-X, where X is a valid base64 string of any length. If a compiler for metaC-X is called with an empty file it will base64 decode X and compile the decoded string, else it will just compile the file.

Implementations

C

/**
 * metagcc-n compiler
 * Copyright (c) 2024 Jannes Althoff
 *
 * This software may be distributed under the terms of the BSD license.
 */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

/*
 * Base64 encoding/decoding (RFC1341)
 * Copyright (c) 2005-2011, Jouni Malinen <j@w1.fi>
 *
 * This software may be distributed under the terms of the BSD license.
 */


static const unsigned char base64_table[65] =
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

/**
 * base64_decode - Base64 decode
 * @src: Data to be decoded
 * @len: Length of the data to be decoded
 * @out_len: Pointer to output length variable
 * Returns: Allocated buffer of out_len bytes of decoded data,
 * or %NULL on failure
 *
 * Caller is responsible for freeing the returned buffer.
 */
unsigned char * base64_decode(const unsigned char *src, size_t len,
			      size_t *out_len)
{
	unsigned char dtable[256], *out, *pos, block[4], tmp;
	size_t i, count, olen;
	int pad = 0;

	memset(dtable, 0x80, 256);
	for (i = 0; i < sizeof(base64_table) - 1; i++)
		dtable[base64_table[i]] = (unsigned char) i;
	dtable['='] = 0;

	count = 0;
	for (i = 0; i < len; i++) {
		if (dtable[src[i]] != 0x80)
			count++;
	}

	if (count == 0 || count % 4)
		return NULL;

	olen = count / 4 * 3;
	pos = out = malloc(olen);
	if (out == NULL)
		return NULL;

	count = 0;
	for (i = 0; i < len; i++) {
		tmp = dtable[src[i]];
		if (tmp == 0x80)
			continue;

		if (src[i] == '=')
			pad++;
		block[count] = tmp;
		count++;
		if (count == 4) {
			*pos++ = (block[0] << 2) | (block[1] >> 4);
			*pos++ = (block[1] << 4) | (block[2] >> 2);
			*pos++ = (block[2] << 6) | block[3];
			count = 0;
			if (pad) {
				if (pad == 1)
					pos--;
				else if (pad == 2)
					pos -= 2;
				else {
					/* Invalid padding */
					free(out);
					return NULL;
				}
				break;
			}
		}
	}

	*out_len = pos - out;
	return out;
}

int
main(int argc, char ** argv)
{
  if(argc > 2)
    {
      fprintf(stderr, "%s [file]\n", argv[0]);
      return -1;
    }
  if(strlen(argv[0])>8)
    {
      size_t bytes;
      unsigned char * byte_array = base64_decode(argv[0]+8, strlen(argv[0]+8), &bytes);
      if(byte_array == NULL)
        {
          fprintf(stderr, "An error happend in base64 decode maybe your trying to programm in an invalid language", argv[0]);
          return -1;
        }
      FILE * fp;
      fp = fopen( argv[1],"r");
      int empty = 0;
      if ( fp == NULL )
        {
          empty = 1;
        }
      else
        {
          fseek (fp, 0, SEEK_END);
          size_t size = ftell(fp);
          if (0 == size)
            {
              empty = 1;
            }
          fclose(fp);
        }
      FILE * fp_tmp;
      if((fp_tmp = fopen(".tmp_metagcc", "w")) == NULL)
        {
          fprintf(stderr, "Could not open tmpfile");
          return -1;
        }
      else
        {
          if(empty)
            {
              fprintf(fp_tmp, "%.*s", bytes, byte_array);
              fclose(fp_tmp);
              system("gcc .tmp_metagcc");
              system("rm .tmp_metagcc");
            }
          else
            {
              char * cmd = malloc(10 + strlen(argv[1]));
              cmd[0] = 'g'; cmd[1] = 'c'; cmd[2] = 'c';
              cmd[3] = ' '; cmd[4] = '\0';
              system(strcat(cmd, argv[1]));
              free(cmd);
            }
        }
    }
    return 0;
}

metaC-n

metaC-LyoqCiAqIG1ldGFnY2MtbiBjb21waWxlcgogKiBDb3B5cmlnaHQgKGMpIDIwMjQgSmFubmVzIEFsdGhvZmYKICoKICogVGhpcyBzb2Z0d2FyZSBtYXkgYmUgZGlzdHJpYnV0ZWQgdW5kZXIgdGhlIHRlcm1zIG9mIHRoZSBCU0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ZnApOwogICAgICAgICAgaWYgKDAgPT0gc2l6ZSkKICAgICAgICAgICAgewogICAgICAgICAgICAgIGVtcHR5ID0gMTsKICAgICAgICAgICAgfQogICAgICAgICAgZmNsb3NlKGZwKTsKICAgICAgICB9CiAgICAgIEZJTEUgKiBmcF90bXA7CiAgICAgIGlmKChmcF90bXAgPSBmb3BlbigiLnRtcF9tZXRhZ2NjIiwgInciKSkgPT0gTlVMTCkKICAgICAgICB7CiAgICAgICAgICBmcHJpbnRmKHN0ZGVyciwgIkNvdWxkIG5vdCBvcGVuIHRtcGZpbGUiKTsKICAgICAgICAgIHJldHVybiAtMTsKICAgICAgICB9CiAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICBpZihlbXB0eSkKICAgICAgICAgICAgewogICAgICAgICAgICAgIGZwcmludGYoZnBfdG1wLCAiJS4qcyIsIGJ5dGVzLCBieXRlX2FycmF5KTsKICAgICAgICAgICAgICBmY2xvc2UoZnBfdG1wKTsKICAgICAgICAgICAgICBzeXN0ZW0oImdjYyAudG1wX21ldGFnY2MiKTsKICAgICAgICAgICAgICBzeXN0ZW0oInJtIC50bXBfbWV0YWdjYyIpOwogICAgICAgICAgICB9CiAgICAgICAgICBlbHNlCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICBjaGFyICogY21kID0gbWFsbG9jKDEwICsgc3RybGVuKGFyZ3ZbMV0pKTsKICAgICAgICAgICAgICBjbWRbMF0gPSAnZyc7IGNtZFsxXSA9ICdjJzsgY21kWzJdID0gJ2MnOwogICAgICAgICAgICAgIGNtZFszXSA9ICcgJzsgY21kWzRdID0gJ1wwJzsKICAgICAgICAgICAgICBzeXN0ZW0oc3RyY2F0KGNtZCwgYXJndlsxXSkpOwogICAgICAgICAgICAgIGZyZWUoY21kKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9Cg== compiles an empty file to a metaC-n compiler.