项目作者: AndroDevcd

项目描述 :
The Sharp programming language
高级语言: C++
项目地址: git://github.com/AndroDevcd/Sharp.git
创建时间: 2016-11-29T21:52:04Z
项目社区:https://github.com/AndroDevcd/Sharp

开源协议:

下载




Sharp Logo

The Sharp Programming Language

Sharp is an open source a modern object-oriented programming language that aims to bridge the gap between modern expressive programming paradigms and strictly typed rigid languages like Java and C#. My goal is to provide the usefulness of an object oriented programming language, while holding the simplicity of an expressive programming language without having to sacrifice performance.

Sharp is elegantly simple.

  1. mod app;
  2. def main() {
  3. print("hello, world!");
  4. }

The syntax in Sharp is simple, easy to read, and beautiful allowing you to focus more on what the code
is doing rather that the syntax

  1. mod app;
  2. def compute() {
  3. // TODO...
  4. }
  5. def main() {
  6. for < 10:
  7. compute();
  8. }

The language library comes standard with a comprehensive lightweight multi-threading library task
that makes handling work in the background simple to put together but looks good as well.

  1. mod app;
  2. // import the threadding library
  3. import std.io;
  4. import std.io.task;
  5. def heavy_computation() {
  6. // simulate heavy work being done
  7. sleep(3000);
  8. }
  9. def lots_of_math() : int {
  10. // simulate heavy work being done
  11. sleep(1000);
  12. return 6;
  13. }
  14. def main() {
  15. // here we create a background task to do some work
  16. launch = { ->
  17. heavy_computation();
  18. }
  19. // launch a task that you can get the answer at a later time
  20. answer := deferred_task<int>.for_result(
  21. with_timeout(1500),
  22. { obs : object ->
  23. ((observable<int>)obs).post(lots_of_math());
  24. }
  25. );
  26. sleep(700);
  27. println("you're answer is ${answer.response}"); // block main thread until answer is posted
  28. }

Sharp supports several convient features that improve the overall experience with programming in the language.

  1. mod app;
  2. class list<T> {
  3. // inferred type system using `:=`
  4. size := 0;
  5. data : T[];
  6. }
  7. /*
  8. * Extension functions
  9. */
  10. def list.last() : T {
  11. return data[size-1];
  12. }
  13. /*
  14. * Function pointers
  15. */
  16. fn main_ptr := main;
  17. /*
  18. * inline function declarations
  19. */
  20. count := 1;
  21. def inc_count() := count++;
  22. /*
  23. * Type aliasing
  24. */
  25. alias var as Dollar
  26. money : Dollar = 1.52;
  27. /*
  28. * Anonymous arrays
  29. */
  30. num_list := { 1, 2, 3 };
  31. /*
  32. * Typed Dictionaries
  33. */
  34. response_messages := { as <int, string>
  35. 200 : "200 error",
  36. 500 : "500 Error",
  37. 405 : "405 Error"
  38. };
  39. /*
  40. * Anonymous Dictionaries
  41. */
  42. days := {
  43. 0 : "Monday",
  44. 1 : "Tuesday",
  45. 2 : "Wednesday",
  46. 3 : "Thursday",
  47. 4 : "Friday",
  48. 5 : "Saturday",
  49. 6 : "Sunday"
  50. };
  51. /*
  52. * Anonymous functions
  53. */
  54. fn total_size := { data : object[] -> (var)
  55. total := 0;
  56. for i < data: {
  57. total += sizeof(data[i]);
  58. }
  59. return total;
  60. };
  61. /*
  62. * Class mutations
  63. *
  64. * Class list now has the field initialSize
  65. * and constructor, class mutation acts just like
  66. * an advanced version of extention functions
  67. */
  68. mutate class list {
  69. initialSize : var = 100;
  70. List(data : T[]) {
  71. // ...
  72. }
  73. }
  74. // and more !!
  75. def main() {
  76. for < 10:
  77. compute();
  78. }

Sharp has a multi-language inspired syntax taking the beauty of languages such as JavaScript, python, Java, Go, and C#. While expressing it in a more concise way. sharp supports anonymous functions that make it easy to abstract away blocks of code without the need to declare a bunch of on-time use functions in your code.

  1. mod app;
  2. def main() {
  3. var num, x;
  4. for(var i = 0; i < 100; i++) {
  5. num = num*Math.sqrt(num)/2;
  6. x++;
  7. }
  8. fn someComputation = { num : var -> (nil)
  9. // we are in a completly seperate code space
  10. // this code space knows nothing of the x variable
  11. // unless explicitly passed in
  12. print("num = " + num);
  13. };
  14. someComputation(num);
  15. }

The above code is a simple illustration of how Sharp’s low friction syntax allows you to evolve your code overtime as your requirements change
from local code, to anonymous function, to a globally accessed static function and more.
Although being an object oriented language, sharp unlike other languages don’t force you to explicitly follow that model of programming.
It’s understood that the old languages of today such as java, c# and more require alot of boilerplate code to be written thus taking away developer time
. Below is just one simple way sharp can remove the menotinous task usually encountered in you day-to-day development flow.

  1. mod app;
  2. // here we create a data class o hold inforation about a vehicle
  3. class Vehicle {
  4. licensePlate := ""; // we dont need to specify types if we dont want to the compiler figures it out for us
  5. wheels := 4; // default value of 4 wheels
  6. }
  7. // here we abstract the vehicle class into a truck
  8. class Truck base Vehicle {
  9. make := "", model = "";
  10. color : Color;
  11. modelYear := 1997;
  12. }
  13. def main() {
  14. car := new Truck {
  15. make = "Ford",
  16. model = "F-150",
  17. color = Color.SPACE_GREY,
  18. modelYear = 2024,
  19. licensePlate = "ZSY 7CH9"
  20. // wheels = 4 // wheels is already set to default value of 4 so we dont need this statement
  21. }; // Object Truck is created inline and assigned the values instead of calling a constructor
  22. // alternativley you can create classes like this as well
  23. veh := new Vehicle {"ZSY 7CH9"}; // creates a vehicle with 4 wheels and a license plate number
  24. }

Getting Started

Thdocumentation below explains how to get you started with using sharp.

Downloading

To download Sharp go to releases and download the latest release of Sharp. Unpack the folder revealing the contents inside. Sharp is currently supporting 64 & 32 bit linux and windows operating systems, below covers how to install sharp on both systems:

Installing on Windows

Navigate to the folder {YOUR_DIR}\release-pkg\bin\x64\win. Create the folder C:/Program Files/Sharp/bin. Place the Sharp binaries in the bin folder. Add following folder to your PATH:

  1. PATH = {OTHER_PATH_DIRS}; C:/Program Files/Sharp/bin/;

Windows Powershell is the preferred method to use sharp but you may use windows CMD as well.
Next cd into the directory of your sharp build to the folder lib and run the commands below using powershell

  1. mkdir "C:/Program Files/Sharp/include/"
  2. cd {SHARP_INSTALL_PATH}/lib/support/0.2.9/
  3. cp -r * "C:/Program Files/Sharp/include/"
  4. Sharpc test.sharp -o test -s -R
  5. Sharp test

After running the commands above if you receive the final output as ``Hello, World!``` Then Sharp has been installed properly and your good to go!

Installing on linux

Navigate to the folder {YOUR_DIR}\release-pkg\. Installing sharp on linux is very simple simply run the command below:

  1. chmod +x linux-install.bash
  2. ./linux-install.bash

After running the commands above if you receive the final output as ``Hello, World!``` Then Sharp has been installed properly and your good to go!

Programming Ideas

After you install Sharp if you are having trouble coming up with programming ideas then you can head over to the ‘examples/‘ directory to see a list
of sample programs using various parts of the Sharp programming language.