Cpy Programming Language - Official Website

Introduce Cpy

What is Cpy?

Cpy is a general-purpose scripting language with a C-like syntax. It is easy to learn, powerful, has lots of modules: string, regex, socket, http, multithread, multiprocess, and so on...

Cpy's familiar syntax just looks like the C programming language, and any other C-like programming languages such as PHP, Java, JavaScript.

Cpy's interpreter is based on Python interpreter, so it inherets all abilities of Python, except for it is ALWAYS RIGHT C-LIKE grammar. Cpy is cross-platform.

Why Cpy?

Cpy is a C-like syntax scripting language, it is intended to be used for Application, general, Web, scripting. Comparison of programming languages

Contact

Any question or any suggustion, please contact: http://www.ideawu.com/blog/cpy

Getting Started

Hello World!

// file: hello.cpy
printf("Hello World!\n");

Save the code above into a file named "hello.cpy", then open cmd.exe, type the command below and press Enter. Run:

$ cpy hello.cpy
Hello World!
$

A bit more complicate sample

// file: stdin.cpy
print "input 'q' to quit:";

while(true){
	printf("> ");
	line = stdin.readline();
	line = line.strip().lower();
	if(line == 'q'){
		print "bye.";
		break;
	}else{
		print 'your input:', repr(line);
	}
}

Run:

$ cpy stdin.cpy
input 'q' to quit:
> a
your input: 'a'
> b
your input: 'b'
> q
bye.

We use both print command and printf() function. The print command output a string to console with a newline. We will discuss more differences between print and printf.

Installing

Installing Cpy is quit easy.

  1. Install Python:

    Download Python Installer[link], choose the right distribution(Python 2.*, not Python 3.*) for your operating system, for Windows users there is MSI. Download the installer, and install Python on your system.

    Run cmd.exe from Start menu, make sure Python is working...

    C:\> python -c "print 1"
    1
    
    C:\>
    

    Until now, everything works fine. Looks good!

  2. Download and Install Cpy:

    Download Cpy Installer[link]. Cpy is cross-platform, so there is only one distribution.

    Unzip the downloaded file, you will get a folder named cpy, open the folder. Copy the folder path from Location Bar, add this path to your system's PATH by editing Environment Variable.

    Make sure Cpy is working...

    C:\> cpy
    Cpy - A C-like scripting language.
    Copyright (c) 2012 ideawu.com
    
    Usage:
        cpy source_file
    

    It works!

Reference

Data types

null, true, false

a = null;
b = true;
c = false;

int

a = 100;

double

a = 100.1;

string

a = "hello";
a += " ";
a += 'world!';
print a; // hello world!

array(list)

a = []; // empty array
a.append(1);
a.append(2);
print a[0]; // output: 1
print a; // output: [1, 2]

a = [1, 2];
print a;

dictionary

a = {}; // empty dictionary
a['x'] = [1, 2];
a['y'] = [3, 4];
foreach(a as k=>v1, v2){
	printf('%s: %d, %d\n', k, v1, v2);
}

# output #
y: 3, 4
x: 1, 2

Functions

function

function f(a, b=1){
	return a + b;
}

print f(1);
print f(1, 2);

# output #
2
3

Classes

class, new

class A{
}

a = new A();

constructor

class A{
	function init(a){
		print 'A init', a;
	}
}

member variables

class A{
	public a = 0;

	function init(a){
		this.a = a;
	}
}

static member variables

class A{
	public static s = 1;
}

print A.s;

member functions

class A{
	public a = 0;
	public static s = 1;
	
	function init(a){
		this.a = a;
		print 'A init', a;
	}

	function f(a, b=1){
		return a + b;
	}
}

print A.s; // 1
a = new A(1); // A init 1
print a.f(1, 2); // 3

Inheritance(extends)

class A{
	function f(){
		print 'A.f';
	}
}
class B extends A{
	function g(){
		print "B.g";
	}
}

b = new B();
b.f();
b.g();
calling overidded(parent's same name) function from child class

NOT SUPPORTED!

TODO:

Control flow statements

if-else

if(condition){
	// ...
}

or

if(condition){
}else{
}

or

if(condition){
}else if(condition2){
... more else if
}

or

if(condition){
}else if(condition2){
... more else if
}else{
}

while, do-while

while(condition){
	//...
}

do{
	//...
}while(condition);

for, foreach

for(init; condition; expr){
	//...
}

foreach(iterable as val){
}

or

foreach(iterable as key=>val){
}

The key variable indicates current value's position in list object, or the key to it of a dictionary. Example:

arr = [10, 11, 12];
foreach(arr as k=>v){
	print k, v;
}

# output: #
0 10
1 11
2 12

#################

d = {
	'a': 1,
	'b': 2,
	'c': 3,
	};

foreach(d as k=>v){
	print k, v;
}

# output: #
a 1
c 3
b 2

switch

Exceptions

try-catch

Multi source files

import

Reusing Python codes

import Python's built-in modules

import Python script files