讲c语言_讲其他语言的故事

讲c语言

重点(Top highlight)

You live a new life for every language you speak. If you know only one language, you live only once.

您使用的每种语言都会过上新生活。 如果您只会一种语言,那么您只会生活一次。

Czech proverb

捷克谚语

I will not talk about spoken language by humans here, programming languages define ways to express what we want the computer to do and show it how it can do it.

在这里,我不会谈论人类的口头语言,编程语言定义了表达我们希望计算机执行的操作并向其展示如何实现的方法。

Back in November 2015 and right after finishing the Ghostblade project I was looking for a new challenge, then finally, the time has come for me to create my own programming language from scratch. During the summer of 2015, I read the specifications of the C# language and I got deep into some concepts like type inference and generics. How they actually work and how OOP operates in depth. This experience was conducted for a test language called V# or Rava, the programming language draft was similar to C# but it included a lot more.

回顾2015年11月,在完成Ghostblade项目之后,我一直在寻找新的挑战,然后终于是时候从头开始创建自己的编程语言了。 在2015年夏季,我阅读了C#语言的规范,并且深入了解了一些类型推断和泛型等概念。 它们实际如何工作以及OOP如何深入运作。 这种体验是针对称为V#或Rava的测试语言进行的,编程语言草案类似于C#,但它包含的内容更多。

When a friend came to me and told me he was writing an operating system using assembly 16 bits and how it is difficult to do. I sensed a challenge in it, Why not create a programming language from scratch, influenced by OOP languages and C/C++ targetting Intel 16 bits processors?

当一个朋友来找我,并告诉我他正在使用16位汇编语言编写操作系统时,很难做到这一点。 我感受到了一个挑战,为什么不从头开始创建一种编程语言,受面向对象16位处理器的OOP语言和C / C ++的影响?

The answer to that question was the Vatu project, kickstarted in December 2015 and finished in February 2016. The language syntax was written using EBNF syntax (Gold parser), the output of this tool is a DFA Lexer and an LALR parser. So I constructed the AST, added the semantic analyzer, created the assembly 16 bits code generator, added the optimizer and finished with a linker.

这个问题的答案是Vatu项目,该项目于2015年12月启动,并于2016年2月完成。语言语法是使用EBNF语法(黄金解析器)编写的,该工具的输出是DFA Lexer和LALR解析器。 因此,我构建了AST,添加了语义分析器,创建了汇编16位代码生成器,添加了优化器并完成了一个链接器。

The programming language was object-oriented, it supported pointers, delegates, generics, operators overload, interrupt definition, multiple calling convention support, extension methods, polymorphism, custom calling convention for system calls and had a feature that never existed in any other programming language: the ability to define custom operators (unary/binary) that the parser nor the lexer knew.

该编程语言是面向对象的,它支持指针,委托,泛型,运算符重载,中断定义,多调用约定支持,扩展方法,多态性,系统调用的自定义调用约定,并且具有任何其他编程语言都没有的功能:定义解析器或词法分析器都知道的自定义运算符(一元/二进制)的能力。

The project was hosted on Github and it had successfully achieved the desired goal, a sample kernel was written using the language.

该项目托管在Github上,并成功实现了预期目标,并使用该语言编写了示例内核。

A sample kernel code:

示例内核代码:

default asm {
"[org 0]";"start:";
"mov ax,cs";
"mov ds,ax";
"mov es,ax";
"mov ax,0x7000";
"mov ss,ax";
"mov sp,ss";
}namespace std;
enum CONSOLE_COLORS{
BLUE = 1,
GREEN = 2,
RED = 4};
struct XA{
int a;
byte b;
};
typedef enum CONSOLE_COLORS CCOLORS;
// get char
byte getc(){
byte c = 0;
asm{
"mov ah, 0";
"int 0x16 ; wait for keypress";
"mov [BP-1], al";
"mov ah, 0x0E";
"int 0x10 ; otherwise, print out the character!";
}

return c;
}
// put char
void putc(byte c){
asm{
"xor cx,cx";
"xor dx,dx";
"xor bx,bx";
}asm{ "mov byte al, [BP+4]";
"mov ah, 0x0E";
"int 0x10 ; otherwise, print out the character!";
}}
// print null terminated string
void print(string msg){int i = 0;
asm{
"xor cx,cx";
"xor dx,dx";
"xor bx,bx";
"xor ax,ax";
}
// 0 end of string
byte c;
while(msg[i] != 0)
{
c = msg[i];
asm{
"mov byte al, [BP-3]";
"mov ah, 0x0E";
"int 0x10";
}
i++; }
} void cls()
{
asm{
"xor cx,cx";
"xor dx,dx";
"xor bx,bx";
"mov ax, 0x0003";
"int 0x10";}
}
void lnprint(string msg){
asm{
"mov al, 0xA";
"mov ah, 0x0E";
"int 0x10 ; otherwise, print out the character!";
"mov al, 0x0D";
"mov ah, 0x0E";
"int 0x10 ; otherwise, print out the character!";
}
print(msg);} void println(string msg){ print(msg);
asm{
"mov al, 0xA";
"mov ah, 0x0E";
"int 0x10 ; otherwise, print out the character!";
"mov al, 0x0D";
"mov ah, 0x0E";
"int 0x10 ; otherwise, print out the character!";
}}
void getstring(string buff){
byte cc = 0;
int i = 0;
cc = getc();
while(cc != 0x0D){
buff[i] = cc;
i++;
cc = getc();
}
buff[i] = 0;}
void print_color(string msg, byte color)
{
int i = 0;
byte c = 0;
asm{
"mov ax,0xb800";
"mov gs,ax";
"push word gs"; // save gs
"mov bx,0";
"mov si,[BP+6]";
"mov ch,[BP+4]";
}loop{asm{
"lodsb" ;
"or al,al";
"jz done";
"mov byte [gs:bx],al";
"mov byte [gs:bx+1],ch";
"add bx,2";
}}
done:
asm{
"pop word gs";
}}namespace String;
use std;override bool operator ==(string b, string a)
{
for(;*a == *b;) { if(*a == 0)
return true; a++;
b++; }

return false;
}
override bool operator >(string a, string b)
{
for(;*a == *b;) { if(*a == 0)
return false; a++;
b++; }
if(*a > *b)
return true;return false;
}
override bool operator <(string a, string b)
{
for(;*a == *b;) { if(*a == 0)
return false; a++;
b++; }
if(*a < *b)
return true;return false;
}
override string operator +(string a, string b)
{
int i = 0;
int j = 0;
while(a[i] != 0)
i++;while(b[j] != 0){
a[i] = b[j];
j++;
i++;
}
a[i] = 0;
return a;} uint strlen(string a)
{
uint i = 0;
while(a[i] != 0)
i++; return i;
} void tostring(bool v)
{
if(v == true)
print("TRUE");
else
print("FALSE");}
void tostring(uint a){
if(a < 10)
putc((byte)(a+48));
else{
tostring(a / 10);
putc(48+(byte)(a % 10)); }
}
void reverse(string str, int length)
{
uint start = 0;
uint end; end = length - 1;
while (start < end)
{
str[start] <> str[end];
start++;
end--;
}
}namespace OPERATIONS;
use std;
use String;
bool isdigit(byte c)
{
if(c >= 48 && c <= 57)
return true;
else return false;
}
uint a2u(string s)
{
int i = 0;
uint num=0;
while(s[i] != 0)
{
if(isdigit(s[i]) == true)
num=(uint)((s[i])-'0')+num*10;
else
println("Not a number");
i++;

} return num;
}private string prompt = ">>";
private string NM_msg = "Type a number : ";
private string buff = "00000"; // 5 digits number
private string OP_msg = "Type an operator (+,-,*,/): ";
const string OP_PL = "+";
const string OP_MI = "-";
const string OP_MUL = "*";
const string OP_DIV = "/";
void operate(){
uint a = 0;
uint b = 0;
uint r = 0;
lnprint(NM_msg);
lnprint(prompt);
getstring(buff);
a = a2u(buff);
lnprint(NM_msg);
lnprint(prompt);
getstring(buff);
b = a2u(buff);
lnprint(OP_msg);
lnprint(prompt);
getstring(buff); if(OP_PL == buff)
r = a+b;
else if(OP_MI == buff)
r = a-b;
else if(OP_DIV == buff)
r = a/b;
else if(OP_MUL == buff)
r = a * b;
else
lnprint("Unknown Operator only (+,*,-,/) are allowed");
lnprint("RESULT : ");
tostring(r);


}namespace VTOS;
use std;
use String;
use OPERATIONS;
const string prompt = ">";
string input = "000000000000000000000000000000000000000000000000000000000";
const string hello_cmd = "hello";
const string about_cmd = "about";
const string op_cmd = "oper";
const string op_help = "help";
const string op_sd = "shutdown";
const string op_res = "restart";
const string op_swap = "s";
const string help_msg = "oper : Operation, help : Help, hello : Hello World, about : About, shutdown, restart";
const string about_text = "This os is a simple demo written to test Vatu compiler";struct XA* buffer[5]; void restart() {
asm{
"int 0x19";
}}
void shutdown() {
asm{
"mov ax, 0x1000";
"mov ax, ss";
"mov sp, 0xf000";
"mov ax, 0x5307";
"mov bx, 0x0001";
"mov cx, 0x0003";
"int 0x15";
}
}
interrupt 0x86
{
uint dest;
dest = AX;
if(dest == 1)
shutdown();
else if(dest == 2)
restart();
}
interrupt 0
{
lnprint("Division par zero");
}void printA(string a) extends string
{
print("Your message was ");
lnprint(a);
print("HGJ");
}
void Hello() static extends void
{
lnprint(" Hello from extend ");
}
entry void main()
{cls();
print_color("Welcome to Vatu OS Kernel", CONSOLE_COLORS.BLUE);
lnprint("Write your command : ");

loop{
lnprint(prompt);
getstring(input);
if(hello_cmd == input)
lnprint("Hello, from Vatu OS");
else if(about_cmd == input)
lnprint(about_text);
else if(op_cmd == input)
operate();
else if(op_help == input){
lnprint(help_msg);
op_help.printA();
void::Hello();
}
else if(op_sd == input){
AX = 1;
interrupt 0x86; }
else if(op_res == input)
{
AX = 2;
asm{"int 86h";}
} else
lnprint("Unknown command");

}}

Vatu was a dream come true and again the harmonic convergence made it happen.

瓦图是一个梦想成真,谐波的融合再次实现了它。

To be continued…

未完待续…

翻译自: https://medium.com/swlh/the-story-speaking-other-languages-fbca1b2563e

讲c语言

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值