Submission details
Task:Tulkki
Sender:j3rmu
Submission time:2025-10-29 12:14:35 +0200
Language:C
Status:COMPILE ERROR

Compiler report

input/code.c: In function 'main':
input/code.c:51:17: warning: implicit declaration of function 'asm' [-Wimplicit-function-declaration]
   51 | #define psh(V)  asm ("push %0" : : "r" (V));
      |                 ^~~
input/code.c:127:17: note: in expansion of macro 'psh'
  127 |                 psh(bc_dec);
      |                 ^~~
input/code.c:51:32: error: expected ')' before ':' token
   51 | #define psh(V)  asm ("push %0" : : "r" (V));
      |                     ~          ^
input/code.c:127:17: note: in expansion of macro 'psh'
  127 |                 psh(bc_dec);
      |                 ^~~
input/code.c:52:30: error: expected ')' before ':' token
   52 | #define pop(V) asm ("pop %0" : "=r" (V) :)
      |                    ~         ^
input/code.c:130:17: note: in expansion of macro 'pop'
  130 |                 pop(bc_dec);
      |                 ^~~

Code

#include <asm-generic/errno.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>

#define OP(A) A >> 24
#define VAR(A) (A >> 16) & 0xFF  
#define OFFSET(A) A & 0xFFFF

enum opco
    {
	CLEAR = 1,
	INCREASE,
	PRINT,
	BRANCH_CND,
	BRANCH_UNC,
	LDLAR,
    };

unsigned int varar[255] = {0};
unsigned int lar[255] = {0};

int32_t prog[1000] = {0};
int pc = 0, pwc = 0;
unsigned char eof = 0;

long getln (char* b, size_t n, FILE* stream)
{
    int c ,rc = 0;
    while (rc < n)
    {
	c = getc(stream);
	if (c <= 0)
	{
	    return -1;
	}
	else if (c == '\n')
	    break;
	b[rc] = (char)(c & 0xFF);
	rc++;
    }
    b[rc] = 0;

    return rc;
}

#define psh(V)	asm ("push %0" : : "r" (V));
#define pop(V) asm ("pop %0" : "=r" (V) :)

void eval (void)
{
    unsigned int inst;

    while ((inst = prog[pc++]))
    {
	switch (OP(inst))
	{
	case CLEAR:
	    varar[VAR(inst)] = 0;
	    break;
	case INCREASE:
	    varar[VAR(inst)]++;
	    break;
	case PRINT:
	    printf("%u ", varar[VAR(inst)]);
	    break;
	case LDLAR:
	    lar[VAR(inst)] = varar[VAR(inst)] + 1;
	case BRANCH_CND:
	    if (!lar[VAR(inst)])
		pc = OFFSET(inst);
	    else
		lar[VAR(inst)]--;
	    break;
	case BRANCH_UNC:
	    pc = OFFSET(inst);
	    break;
	}
    }
}

int main (void)
{
    char ib[100];
    unsigned int opc = 0;
	
    ungetc(getc(stdin), stdin);

    fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
 nl:
    while (getln(ib, sizeof(ib), stdin) != -1)
    { 
	char *op, *var; 
	unsigned long bc_dec = 0; 

	op = strtok(ib, " ");
	do
	{
	    
	    if (op == NULL)
		break;

	    switch (op[0]) 
	    { 
	    case 'C': 
		var = strtok(NULL, " "); 
		opc = CLEAR << 24 | var[0] << 16; 
		break; 
	    case 'I': 
		var = strtok(NULL, " "); 
		opc = INCREASE << 24 | var[0] << 16; 
		break; 
	    case 'P': 
		var = strtok(NULL, " "); 
		opc = PRINT << 24 | var[0] << 16; 
		break; 
	    case 'R': 
		var = strtok(NULL, " "); 
		opc = LDLAR << 24 | var[0] << 16;
		prog[pwc++] = opc;
		opc = BRANCH_CND << 24 | var[0] << 16; 
		bc_dec = BRANCH_UNC << 24 | var[0] << 16 | pwc; 
		psh(bc_dec); 
		break; 
	    case ')': 
		pop(bc_dec); 
		opc = bc_dec; 
		prog[OFFSET(opc)] |= (pwc + 1) & 0xFFFF;
		break;
	    case '#':
		goto nl;
	    default:
		opc = 0;
	    } 

	    if (opc != 0) 
		prog[pwc++] = opc; 
	} while ((op = strtok(NULL, " ")));
    }

    eval();
    
    return 0;
}