1 #target WASI // We are not really targetting WASI, but this way the compiler will output
   2              // WebAssembly code compatible with Firefox 52.
   3 
   4 Function get_length_of_the_first_number() Which Returns Integer32 Is External;
   5 Function get_digit_of_the_first_number_at(Integer32 index) Which Returns Character Is External;
   6 Function get_length_of_the_second_number() Which Returns Integer32 Is External;
   7 Function get_digit_of_the_second_number_at(Integer32 index) Which Returns Character Is External;
   8 Function putc(Character char) Which Returns Nothing Is External;
   9 Function logString(PointerToCharacter string) Which Returns Nothing Is External;
  10 
  11 Character matrix[100*100];
  12 Function f(Integer16 i, Integer16 j) Which Returns Integer32 Does
  13 	Return i * 100 + j;
  14 EndFunction
  15 
  16 Character result[100];
  17 
  18 Function get_length_of_the_result() Which Returns Integer16 Does
  19 	Integer16 i := 0;
  20 	While i < 100 Loop
  21 		If result[i]=0 Then
  22 			Return i;
  23 		EndIf
  24 		i += 1;
  25 	EndWhile
  26 	Return i;
  27 EndFunction
  28 
  29 Function prepend_a_character_to_the_result(Character char) Which Returns Nothing Does
  30 	Integer16 i := get_length_of_the_result() + 1;
  31 	While i > 0 Loop
  32 		result[i]:=result[i - 1];
  33 		i -= 1;
  34 	EndWhile
  35 	result[0] := char;
  36 EndFunction
  37 
  38 Function multiply() Which Returns Nothing Does
  39 	// Initializing the matrix...
  40 	Integer32 i := 0, j := 0;
  41 	While i < get_length_of_the_second_number() Loop
  42 		j := 0;
  43 		While j < get_length_of_the_first_number() + get_length_of_the_second_number() + 1 Loop
  44 			matrix[f(i,j)] := ' ';
  45 			j += 1;
  46 		EndWhile
  47 		i += 1;
  48 	EndWhile
  49 	logString("The matrix is successfully filled with spaces.");
  50 
  51 	//Filling up the matrix...
  52 	Integer32 pointer_to_the_digit_in_the_second_number := 0;
  53 	While pointer_to_the_digit_in_the_second_number < get_length_of_the_second_number() Loop
  54 		Integer16 carry := 0,
  55 			pointer_to_the_matrix_column := get_length_of_the_first_number() +
  56 				pointer_to_the_digit_in_the_second_number + 1;
  57 		Integer16 pointer_to_the_digit_in_the_first_number := get_length_of_the_first_number() - 1;
  58 		While pointer_to_the_digit_in_the_first_number >= 0 Loop
  59 			Integer16 product :=
  60 				(get_digit_of_the_first_number_at(pointer_to_the_digit_in_the_first_number) - '0') *
  61 				(get_digit_of_the_second_number_at(pointer_to_the_digit_in_the_second_number) - '0') +
  62 				carry;
  63 			matrix[f(pointer_to_the_digit_in_the_second_number, pointer_to_the_matrix_column)]
  64 				:= mod(product, 10) + '0';
  65 			carry := product / 10;
  66 			pointer_to_the_matrix_column -= 1;
  67 			pointer_to_the_digit_in_the_first_number -= 1;
  68 		EndWhile
  69 		If carry Then
  70 			matrix[f(pointer_to_the_digit_in_the_second_number,pointer_to_the_matrix_column)] :=
  71 				mod(carry, 10) + '0';
  72 		EndIf
  73 		If carry >= 10 Then
  74 			pointer_to_the_matrix_column -= 1;
  75 			matrix[f(pointer_to_the_digit_in_the_second_number, pointer_to_the_matrix_column)] := 
  76 				carry / 10 + '0';
  77 		EndIf
  78 		pointer_to_the_digit_in_the_second_number += 1;
  79 	EndWhile
  80 	logString("The matrix is now filled with the products of the first number and the digits of the second.");
  81 	
  82 	//Summing up the matrix...
  83 	result[0]:=0;
  84 	Integer16 carry := 0;
  85 	i := get_length_of_the_first_number() + get_length_of_the_second_number();
  86 	While i >= 0 Loop
  87 		Integer32 sum := carry;
  88 		j := 0;
  89 		While j < get_length_of_the_second_number() Loop
  90 			If not(matrix[f(j, i)] = ' ') Then
  91 				sum += matrix[f(j, i)] - '0';
  92 			EndIf
  93 			j += 1;
  94 		EndWhile
  95 		prepend_a_character_to_the_result(mod(sum, 10) + '0');
  96 		carry := sum / 10;
  97 		i -= 1;
  98 	EndWhile
  99 	If carry Then
 100 		prepend_a_character_to_the_result(carry + '0');
 101 	EndIf
 102 	logString("The matrix is now summed up and the product of the two numbers is calculated.");
 103 
 104 	//Replace the trailing zeros with spaces. Now that's a lot easier to do in AEC than in JavaScript...
 105 	i := 0;
 106 	While result[i] = '0' Loop
 107 		result[i] := ' ';
 108 		i += 1;
 109 	EndWhile
 110 	logString("The trailing zeros in the result are now replaced by spaces.");
 111 
 112 	//Print the result...
 113 	putc(' ');
 114 	putc(' ');
 115 	i := 0;
 116 	While i < get_length_of_the_first_number() Loop
 117 		putc(get_digit_of_the_first_number_at(i));
 118 		i += 1;
 119 	EndWhile
 120 	putc('*');
 121 	i := 0;
 122 	While i < get_length_of_the_second_number() Loop
 123 		putc(get_digit_of_the_second_number_at(i));
 124 		i += 1;
 125 	EndWhile
 126 	putc('\n');
 127 	
 128 	i := 0;
 129 	While i < get_length_of_the_first_number() + get_length_of_the_second_number() + 1 + 2 Loop
 130 		putc('-');
 131 		i += 1;
 132 	EndWhile
 133 	putc('\n');
 134 
 135 	i := 0;
 136 	While i < get_length_of_the_second_number() Loop
 137 		j := 0;
 138 		While j < get_length_of_the_first_number() + get_length_of_the_second_number() + 1 Loop
 139 			putc(matrix[f(i,j)]);
 140 			j += 1;
 141 		EndWhile
 142 		putc('\n');
 143 		i += 1;
 144 	EndWhile
 145 	
 146 	i := 0;
 147 	While i < get_length_of_the_first_number() + get_length_of_the_second_number() + 1 Loop
 148 		putc('-');
 149 		i += 1;
 150 	EndWhile
 151 	putc('\n');
 152 
 153 	i := 0;
 154 	While result[i] Loop
 155 		putc(result[i]);
 156 		i += 1;
 157 	EndWhile
 158 	putc('\n');
 159 
 160 	logString("The result should be visible on the screen now!");
 161 EndFunction
 162