الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

ابي اعمل Makefile لهذا البرنامج

بدأه albronzy في 23 أبريل 2009 · 1 رد · 532 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم و رحمة الله و بركاته,,,

ابي خدمه بسيطه و ان شاء الله ما تردوني

ابي اعمل make file لهذا البرنامج كل شي جاهز بس الميك فايل ما اعرف اسويه

هنا رابط تحميل الملف

http://rs761.rapidshare.com/files/224633286/nectj.java

او هذا الكود

import java.io.*;
import java.util.*;

// Class nectj to implement the interpretere
public class nectj
{
	int MAX = 1000;
	String fName, program;
	HashMap<String, String> hashMap;// hashmap for storing program variables
	Scanner sc;

	String []tokens;// collect tokens here
	int numTokens;

	nectj( String sFile )
	{
		hashMap = new HashMap<String, String>();
		sc = new Scanner( System.in );
		fName = sFile;
		program = "";
		numTokens = 0;
	}

	// Tokenize the whole source file
	void tokenizeSource()
	{
		try
		{
			// Read the source file
			BufferedReader br = new BufferedReader( new FileReader( fName ) );
			String line;
			while( ( line = br.readLine() ) != null )
			{
				int ind = line.indexOf( "//" );
				if( ind >= 0 )
				{
					line = line.substring( 0, ind );
				}
				line = line.trim();
				if( line.length() > 0 )
				{
					program += line + "\n";
				}
			}

			// separate program into tokens
			String []temp = new String[10000];
			for (int j = 0; j < program.length(); j++ ) 
			{
				int i = 0;
				char []t = new char[100];
				if( Character.isLetter( program.charAt(j) ) )
				{
					while( j < program.length() && Character.isLetterOrDigit( program.charAt(j) ) )
					{
						t[i++] = Character.toLowerCase( program.charAt(j++) );
					}
				}
				else if( Character.isDigit( program.charAt(j) ) )
				{
					while( j < program.length() && Character.isDigit( program.charAt(j)) )
					{
						t[i++] = program.charAt(j++);
					}
				}
				else if( program.charAt(j) == '"' )
				{
					t[i++] = program.charAt(j++);
					while( j < program.length() && program.charAt(j) != '"' )
					{
						t[i++] = program.charAt(j++);
					}
					t[i++] = program.charAt(j++);
				}
				else
				{
					t[i++] = program.charAt(j++);
				}
				j--;
				String token = new String( t, 0, i ).trim();
				if( token.length() > 0 )
				{
					temp[numTokens++] = token;
				}
			}
			tokens = new String[numTokens];
			for( int k = 0; k < numTokens; k++ )
				tokens[k] = temp[k];
		}
		catch( Exception e ){}
	}

	// Run a specified function
	void runFunction( String function, String label )
	{
		for( int i = 0; i < numTokens - 1; i++ )
		{
			if( tokens.equals( "func" ) && tokens[i+1].equals( function ) )
			{
				while( tokens.equals( ")" ) == false )
					++i;
				++i;
				while( tokens.equals( "endfunc" ) == false )
				{
					String []temp = new String[MAX];
					int cnt = 0;
					while( tokens.equals( ";" ) == false )
					{
						temp[cnt++] = tokens[i++];
					}
					++i;

					String []statement = new String[cnt];
					for( int k = 0; k < cnt; k++ )
					{
						statement[k] = temp[k];
					}
					if( label.length() > 0 )
					{
						if( statement[0].equals( label ) == false )
							continue;
						label = "";
					}
					String ret = runStatement( statement );
					if( ret.length() > 0 )
					{
						runFunction( function, ret );
						return;
					}
				}
				return;
			}
		}
	}

	// Gets the parameters of a function
	String []getParmas( String function )
	{
		String []parmas = null;
		for( int i = 0; i < numTokens - 1; i++ )
		{
			if( tokens.equals( "func" ) && tokens[i+1].equals( function ) )
			{
				String []temp = new String[100];
				int cnt = 0, j = i + 3;
				while( tokens[j].equals( ")" ) == false )
				{
					if( tokens[j].equals( "," ) == false )
						temp[cnt++] = tokens[j];
					j++;
				}
				parmas = new String[cnt];
				for( int k = 0; k < cnt; k++ )
					parmas[k] = temp[k];
				break;
			}
		}
		return parmas;
	}

	// Executes a statement
	String runStatement( String []stmt )
	{
		if( stmt[0].equals( "read" ) ) // Read statement
		{
			String value = sc.nextLine();
			hashMap.put( stmt[2], value );
		}
		else if( stmt[0].equals( "write" ) ) // Write statement
		{
			if( stmt[2].charAt(0) == '"' )
			{
				System.out.println( stmt[2].substring( 1, stmt[2].length() - 1 ) );
			}
			else
			{
				String value = hashMap.get( stmt[2] );
				System.out.println( value );
			}
		}
		else if( stmt[0].equals( "if" ) ) // If statement
		{
			String test = stmt[2];
			if( test.equals( "true" ) )
				return stmt[5];
			String id1 = stmt[4];
			String id2 = stmt[6];
			id1 = hashMap.get( id1 );
			id2 = hashMap.get( id2 );
			String ret = stmt[10];
			if( test.equals( "eq" ) )
			{
				if( Integer.parseInt( id1 ) == Integer.parseInt( id2 ) )
					return ret;
			}
			else if( test.equals( "ne" ) )
			{
				if( Integer.parseInt( id1 ) != Integer.parseInt( id2 ) )
					return ret;
			}
			else if( test.equals( "lt" ) )
			{
				if( Integer.parseInt( id1 ) < Integer.parseInt( id2 ) )
					return ret;
			}
			else if( test.equals( "le" ) )
			{
				if( Integer.parseInt( id1 ) <= Integer.parseInt( id2 ) )
					return ret;
			}
			else if( test.equals( "gt" ) )
			{
				if( Integer.parseInt( id1 ) > Integer.parseInt( id2 ) )
					return ret;
			}
			else if( test.equals( "ge" ) )
			{
				if( Integer.parseInt( id1 ) >= Integer.parseInt( id2 ) )
					return ret;
			}
		}
		else
		{
			if( stmt[1].equals( ":" ) ) // Labeled Statement
			{
				String []nStmt = new String[stmt.length - 2];
				for( int i = 2; i < stmt.length; i++ )
					nStmt[i - 2] = stmt;
				return runStatement( nStmt );
			}
			if( stmt.length > 4 && Character.isLetter( stmt[2].charAt( 0 ) ) && stmt[3].equals( "(" ) ) // Funciton call
			{
				if( stmt[4].equals( ")" ) == false ) // At least one parameter
				{
					String []fParams = getParmas( stmt[2] );
					String []aParams = new String[fParams.length];
					int pi;
					for( pi = 0; pi < aParams.length; pi++ )
					{
						aParams[pi] = stmt[ 2*pi + 4 ];
					}
					for( pi = 0; pi < aParams.length; pi++ )
					{
						hashMap.put( fParams[pi], hashMap.get( aParams[pi] ) );
					}
				}
				runFunction( stmt[2], "" );
				String retur = hashMap.get( stmt[2] );
				hashMap.put( stmt[0],  retur );
			}
			else // Expression
			{
				String expr = "";
				for( int i = 2; i < stmt.length; i++ )
					expr += stmt;
				postfix inToPost = new postfix( expr );
				String post = inToPost.convert();

				Stack<Integer> stack = new Stack<Integer>();
				StringTokenizer stk = new StringTokenizer( post, "." );
				while( stk.hasMoreTokens() )
				{
					String token = stk.nextToken();
					if( Character.isLetter( token.charAt(0) ) )
					{
						stack.push( Integer.parseInt( hashMap.get( token )) );
					}
					else if( Character.isDigit( token.charAt(0) ) )
					{
						stack.push( Integer.parseInt( token ) );
					}
					else
					{
						int op2 = stack.pop();
						int op1 = stack.pop();
						if( token.equals( "+" ) )
							stack.push( op1 + op2 );
						else if( token.equals( "" ) )
							stack.push( op1 - op2 );
						else if( token.equals( "*" ) )
							stack.push( op1 * op2 );
						else if( token.equals( "/" ) )
							stack.push( op1 / op2 );
						else if( token.equals( "%" ) )
							stack.push( op1 % op2 );
					}
				}
				String val = "" + stack.pop();
				hashMap.put( stmt[0], val );
			}
		}
		return "";
	}

	void executeMain()
	{
		runFunction( "main", "" );
	}

	public static void main( String args[] )
	{
		if( args.length < 1 )
		{
			System.out.println("Invalid no of arguments.");
		}
		else
		{
			nectj program = new nectj( args[0] );
			program.tokenizeSource();
			program.executeMain();
		}
	}

	// Inner Class to convert into postfix notation
	class postfix 
	{
		Stack<String> theStack;
		String input;
		String output = "";

		public postfix(String in) 
		{
			input = in;
			theStack = new Stack<String>();
		}

		public String convert() 
		{
			for (int j = 0; j < input.length(); j++) 
			{
				int i = 0;
				char []t = new char[100];
				if( Character.isLetter( input.charAt(j) ) )
				{
					while( j < input.length() && Character.isLetterOrDigit( input.charAt(j) ) )
					{
						t[i++] = input.charAt(j++);
					}
				}
				else if( Character.isDigit( input.charAt(j) ) )
				{
					while( j < input.length() && Character.isDigit( input.charAt(j)) )
					{
						t[i++] = input.charAt(j++);
					}
				}
				else
				{
					t[i++] = input.charAt(j++);
				}
				j--;

				String token = new String( t, 0, i ).trim();
				if( token.equals("+") || token.equals("-") )
				{
					gotOper(token, 1);
				}
				else if( token.equals("*") || token.equals("/") || token.equals("%") )
				{
					gotOper(token, 2);
				}
				else if( token.equals("(") )
				{
					theStack.push(token);
				}
				else if( token.equals(")") )
				{
					gotParen( token );
				}
				else if( token.length() > 0 )
				{
					output = output + token + ".";
				}
			}
			while( !theStack.isEmpty() ) 
			{
				output = output + theStack.pop() + ".";
			}
			return output; // return postfix
		}

		public void gotOper(String opThis, int prec1) 
		{
			while ( !theStack.isEmpty() ) 
			{
				String opTop = theStack.pop();
				if ( opTop.equals( "(" ) )
				{
					theStack.push(opTop);
					break;
				}// it's an operator
				else // precedence of new op
				{
					int prec2;
					if ( opTop.equals("+") || opTop.equals("-") )
						prec2 = 1;
					else
						prec2 = 2;
					if (prec2 < prec1) // if prec of new op less than prec of old
					{
						theStack.push(opTop); // save newly-popped op
						break;
					}
					else // prec of new not less
						output = output + opTop + "."; // than prec of old
				}
			}
			theStack.push(opThis);
		}

		public void gotParen(String ch)
		{
			while (!theStack.isEmpty()) 
			{
				String chx = theStack.pop();
				if( chx.equals("(") )
					break;
				else
					output = output + chx + ".";
			}
		}
	}
}

تحياتي لكم , و الشكر مقدماُ

#2

ابحث في المنتدى ستجد الكثير عن الملفات وكيفية قراءتها وكتابتها

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

مواضيع مشابهة